An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20130102/6c5760bc/attachment.pl>
formal vs. passed args: parent.frame() behavior.
2 messages · Jeff Ryan, Peter Dalgaard
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