[R] Appropriate method for sharing data across functions

Hadley Wickham
2012-04-05T20:32:16Z

Why not pass around a reference class?
Hadley

On Thu, Apr 5, 2012 at 3:20 PM, John C Nash <nashjc at uottawa.ca>
wrote:
> In trying to streamline various optimization functions, I would like to
have
> a scratch pad of working data that is shared across a number of functions.
> These can be called from different levels within some wrapper functions for
> maximum likelihood and other such computations. I'm sure there are
other
> applications that could benefit from this.
>
> Below are two approaches. One uses the <<- assignment to a structure
I call
> OPCON. The other attempts to create an environment with this name, but
> fails. Though I have looked at a number of references, I have so far not
> found an adequate description of how to specify where the OPCON environment
> is located. (Both the green and blue books do not cover this topic, at
least
> not under "environment" in the index.)
>
> Is there a recommended approach to this? I realize I could use argument
> lists, but they get long and tedious with the number of items I may need to
> pass, though passing the OPCON structure in and out might be the proper
way.
> An onAttach() approach was suggested by Paul Gilbert and tried, but it has
> so far not succeeded and, unfortunately, does not seem to be usable from
> source() i.e., cannot be interpreted but must be built first.
>
> JN
>
> Example using <<-
>
> rm(list=ls())
> optstart<-function(npar){ # create structure for optimization
computations
> ? # npar is number of parameters ?? test??
> ? OPCON<<-list(MAXIMIZE=TRUE, PARSCALE=rep(1,npar), FNSCALE=1,
> ? ? ? KFN=0, KGR=0, KHESS=0)
> ? # may be other stuff
> ? ls(OPCON)
> }
>
> add1<-function(){
> ? OPCON$KFN<<-1+OPCON$KFN
> ? test<-OPCON$KFN
> }
>
> OPCON<<-list(MAXIMIZE=TRUE, PARSCALE=rep(1,4), FNSCALE=1,
> ? ? ? KFN=0, KGR=0, KHESS=0)
> ls(OPCON)
> print(add1())
> print(add1())
> print(ls.str())
>
> rm(OPCON) # Try to remove the scratchpad
> print(ls())
>
> tmp<-readline("Now try from within a function")
> setup<-optstart(4) # Need to sort out how to set this up appropriately
> cat("setup =")
> print(setup)
>
> print(add1())
> print(add1())
>
> rm(OPCON) # Try to remove the scratchpad
>
> =====================> Example (failing) using new.env:
>
> rm(list=ls())
> optstart<-function(npar){ # create structure for optimization
computations
> ? # npar is number of parameters ?? test??
> ? OPCON<-new.env(parent=globalenv())
> ? OPCON<-list(MAXIMIZE=TRUE, PARSCALE=rep(1,npar), FNSCALE=1,
> ? ? ? KFN=0, KGR=0, KHESS=0)
> ? # may be other stuff
> ? ls(OPCON)
> }
>
> add1<-function(){
> ? OPCON$KFN<-1+OPCON$KFN
> ? test<-OPCON$KFN
> }
>
> OPCON<-new.env(parent=globalenv())
> OPCON<-list(MAXIMIZE=TRUE, PARSCALE=rep(1,4), FNSCALE=1,
> ? ? ? KFN=0, KGR=0, KHESS=0)
> ls(OPCON)
> print(add1())
> print(add1())
> print(ls.str())
>
> rm(OPCON) # Try to remove the scratchpad
> print(ls())
>
> tmp<-readline("Now try from within a function")
> setup<-optstart(4) # Need to sort out how to set this up appropriately
> cat("setup =")
> print(setup)
>
> print(add1())
> print(add1())
>
> rm(OPCON) # Try to remove the scratchpad
>
> ______________________________________________
> 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.



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University