[R] 'deparse(substitute'))' then 'assign' in a S4 methods

William Dunlap wdunlap at tibco.com
Mon Feb 6 18:00:26 CET 2012


The interrupt issue is somewhat orthogonal to the SV4/scoping issue
you asked about.  Trying to put things in the callers frame is
very tricky.  E.g., SV4 methods can insert new frames between
your code and the caller.  Perhaps you could try the following
sort of construct, in which your desired function is inside a
wrapper, whose only job is to collect results from the inner function.
Then make the outer function your SV4 method.

f <- function (n) {
    retval <- NULL # just a placeholder so <<- from f_inner lands here
    f_inner <- function(n) {
        on.exit({
            retval <<- retval_inner # or could use assign(env=parent.env(), ...)
            message("stashed retval_inner as retval")
        })
        retval_inner <- 100
        tryCatch(
            expr = for (i in seq_len(n)) {
                       Sys.sleep(1)
                       retval_inner <- retval_inner + 1
                   },
            interrupt = function(...) message("interrupted in iteration ", i))
    }
    f_inner(n)
    retval
}

E.g.,

  > r <- f(5)
  stashed retval_inner as retval
  > r
  [1] 105
  > rm(r)
  > r <- f(50) # hit interrupt key after a few seconds
  interrupted in iteration 4
  stashed retval_inner as retval
  > r
  [1] 103
  > sapply( 5:1, f) # hit interrupt key sporadically
  stashed retval_inner as retval
  interrupted in iteration 2
  stashed retval_inner as retval
  interrupted in iteration 3
  stashed retval_inner as retval
  interrupted in iteration 2
  stashed retval_inner as retval
  stashed retval_inner as retval
  [1] 105 101 102 101 101

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cgenolin
> Sent: Monday, February 06, 2012 5:13 AM
> To: r-help at r-project.org
> Subject: Re: [R] 'deparse(substitute'))' then 'assign' in a S4 methods
> 
> Thanks Bill,
> 
> In France, there is a famous joke about politician: "Tell me what you need,
> I will explain you how to do without"... But in my specific case, I can not
> use the classical <- and [, it is why I ask my question.
> 
> More precisely, my fonction fooBis can be very long to end. So I want to
> update the value of my variable from time to time.
> 'assign' + break will work, whereas `fooBis<-` <- function(x, value) { x <-
> 4 ; for(i in 1:10000000){};x }  + break will not work.
> 
> Christophe
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/deparse-substitute-then-assign-in-a-S4-
> methods-tp4356748p4361210.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list