Happy 2013, Day 2. I can't seem to figure out why parent.frame() works differently depending on whether it is a formal/default argument or a passed argument. ##### code: basic setup #### tmp <- tempfile() A <- 101 save(A,file=tmp);rm(A) # these work as expected, loading into the parent of the call load() load(tmp);str(A);rm(A) load(tmp, parent.frame());str(A);rm(A) load(tmp, environment());str(A);rm(A) # but these are odd, using local() # works local( { load(tmp); str(A) } ) # FAILS even though env=parent.frame() by default !?!! local( { load(tmp,env=parent.frame()); str(A) } ) # works as well! local( { load(tmp,env=environment()); str(A) } ) ls() ## NOT in .GlobalEnv, correct! args(load) ## env=parent.frame() by default, but is it??? My question is why parent.frame() can't be specified in the args without changing the behavior of the call itself if you aren't at the top level. What am I missing? Jeff ##### output #####> tmp <- tempfile() > A <- 101 > save(A,file=tmp);rm(A) > > # these work as expected, loading into the parent of the call load() > load(tmp);str(A);rm(A)num 101> load(tmp, parent.frame());str(A);rm(A)num 101> load(tmp, environment());str(A);rm(A)num 101> > > > # but these are odd, using local() > > # works > local( { load(tmp); str(A) } )num 101> > # fails even though env=parent.frame() by default !?!! > local( { load(tmp,env=parent.frame()); str(A) } )Error in str(A) : object 'A' not found> > # works as well! > local( { load(tmp,env=environment()); str(A) } )num 101> > ls() ## NOT in .GlobalEnv, correct![1] "tmp"> args(load) ## env=parent.frame() by default, but is it???function (file, envir = parent.frame()) NULL>-- Jeffrey Ryan jeffrey.ryan@lemnica.com www.lemnica.com [[alternative HTML version deleted]]
peter dalgaard
2013-Jan-03 07:21 UTC
[Rd] formal vs. passed args: parent.frame() behavior.
On Jan 3, 2013, at 05:28 , Jeff Ryan wrote:> Happy 2013, Day 2. > > I can't seem to figure out why parent.frame() works differently depending > on whether it is a formal/default argument or a passed argument.Because defaults are evaluated in the function's evaluation environment, whereas passed arguments are evaluated in the caller's environment. Notice in particular, that defaults can refer to other arguments (as in probability=!freq in hist.default) and even to internal variables occasionally.> > ##### code: basic setup #### > > tmp <- tempfile() > A <- 101 > save(A,file=tmp);rm(A) > > # these work as expected, loading into the parent of the call load() > load(tmp);str(A);rm(A) > load(tmp, parent.frame());str(A);rm(A) > load(tmp, environment());str(A);rm(A) > > > > # but these are odd, using local() > > # works > local( { load(tmp); str(A) } ) > > # FAILS even though env=parent.frame() by default !?!! > local( { load(tmp,env=parent.frame()); str(A) } ) > > # works as well! > local( { load(tmp,env=environment()); str(A) } ) > > ls() ## NOT in .GlobalEnv, correct! > args(load) ## env=parent.frame() by default, but is it??? > > > My question is why parent.frame() can't be specified in the args without > changing the behavior of the call itself if you aren't at the top level. > > What am I missing? > > Jeff > > > ##### output ##### > >> tmp <- tempfile() >> A <- 101 >> save(A,file=tmp);rm(A) >> >> # these work as expected, loading into the parent of the call load() >> load(tmp);str(A);rm(A) > num 101 >> load(tmp, parent.frame());str(A);rm(A) > num 101 >> load(tmp, environment());str(A);rm(A) > num 101 >> >> >> >> # but these are odd, using local() >> >> # works >> local( { load(tmp); str(A) } ) > num 101 >> >> # fails even though env=parent.frame() by default !?!! >> local( { load(tmp,env=parent.frame()); str(A) } ) > Error in str(A) : object 'A' not found >> >> # works as well! >> local( { load(tmp,env=environment()); str(A) } ) > num 101 >> >> ls() ## NOT in .GlobalEnv, correct! > [1] "tmp" >> args(load) ## env=parent.frame() by default, but is it??? > function (file, envir = parent.frame()) > NULL >> > > -- > Jeffrey Ryan > jeffrey.ryan at lemnica.com > > www.lemnica.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Possibly Parallel Threads
- Behavior or as.environment in function arguments/call (and force() behaviors...)
- Non-GPL C (or R) inside of a package
- Model object, when generated in a function, saves entire environment when saved
- [Patch suggestion] Adding 3rd arg to tempfile() to set extension
- Model object, when generated in a function, saves entire environment when saved