A followup: doing an assignment of the value works even after the return: > f function( H ) { cat('Evaluate H to get \n') print(H) return(environment()) } > env <- f( function(x) x^2 ) Evaluate H to get function(x) x^2 > env$H <promise: 0117C2D0> > env$H(1) Error: attempt to apply non-function > H <- env$H > H(1) [1] 1
So the oddity is that I can evaluate H within f() without any problem, but outside of f() I need extra work before I can do it. Duncan Murdoch On Tue, 08 Mar 2005 18:00:10 +0000, Duncan Murdoch <[EMAIL PROTECTED]> wrote : >On Tue, 8 Mar 2005 17:44:41 +0000 (GMT), Prof Brian Ripley ><[EMAIL PROTECTED]> wrote : > >>The following note in ?force may help >> >>Note: >> >> 'force' does not force the evaluation of promises. >> >>It is there because people have been confused before. > >Yes, but it also says that it "forces the evaluation of a function >argument", which is what I was trying to do. > >But mainly what may be a bug is the fact that H was available in env >but its value was not, even though it had already been evaluated in >that environment. > >My examples were unnecessarily complicated last time, because they >were too much like the original. Here are simpler versions: > >> f <- function( H ) { >+ cat('Evaluate H to get ', H, '\n') >+ return(environment()) >+ } >> >> env <- f( 1 ) >Evaluate H to get 1 >> env$H ><promise: 0118BF1C> >> >> g <- function( H ) { >+ force(H) >+ return(environment()) >+ } >> >> env <- g( 2 ) >> env$H ><promise: 0118A148> >> >> >> h <- function( H ) { >+ H <- H >+ return(environment()) >+ } >> >> env <- h( 3 ) >> env$H >[1] 3 > >Duncan Murdoch >> >> >>On Tue, 8 Mar 2005, Duncan Murdoch wrote: >> >>> I'm working on a function that does adaptive sampling, and I thought >>> it would be handy to return the function's environment as part of the >>> result so that I could re-use local variables in a subsequent run. My >>> first try didn't work, and it came down to code like this: >>> >>>> f <- function( H, prevEnv = NULL) { >>> + if (!is.null(prevEnv)) H <- prevEnv$H >>> + cat('Evaluate H to get ', H(1), '\n') >>> + return(environment(NULL)) >>> + } >>> >>> I thought that evaluating H would force it, so that H would be >>> available in the environment returned by the function. But this is >>> not so: >>> >>>> env <- f( function(x) x^2 ) >>> Evaluate H to get 1 >>>> env$H >>> <promise: 012094D8> >>>> env$H(1) >>> Error: attempt to apply non-function >>> >>> So I tried to explicitly force it: >>> >>>> g <- function( H, prevEnv = NULL) { >>> + if (!is.null(prevEnv)) H <- prevEnv$H >>> + force(H) >>> + return(environment(NULL)) >>> + } >>> >>> but this still doesn't work: >>> >>>> env <- g( function(x) x^2 ) >>>> env$H >>> <promise: 01206FC0> >>>> env$H(1) >>> Error: attempt to apply non-function >>> >>> It seems that I need to do an assignment to convert H from a promise >>> to an evaluated object: >>> >>>> h <- function( H, prevEnv = NULL) { >>> + if (!is.null(prevEnv)) H <- prevEnv$H >>> + H <- H >>> + return(environment(NULL)) >>> + } >>>> env <- h( function(x) x^2 ) >>>> env$H >>> function(x) x^2 >>>> env$H(1) >>> [1] 1 >>> >>> Is this a bug, or just the way things are? >>> >>> I get the same results in both R-patched and R-devel. > >______________________________________________ >R-devel@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ R-devel@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-devel