[R] do.call in "with" construction

Joshua Wiley jwiley.psych at gmail.com
Mon Jul 25 16:40:53 CEST 2011


Dear Johannes,

because myFun has an environment, and that the global environment.  Look at:

with(myEnv,
{
    myFun2 <- function(symbols){do.call("print", lapply(symbols, FUN=as.name))}
    do.call("myFun2", list(symbols.env))
    do.call("myFun", list(symbols.env))
})

Also, for example, compare:

environment(myFun)
environment(mean)

Hope this helps,

Josh

On Mon, Jul 25, 2011 at 2:23 AM, Johannes Egner
<johannes.egner at gmail.com> wrote:
> Dear all,
>
> I'd appreciate any help to rectify what must be a misconception of mine how
> environments work:
>
> ##########################
> myEnv <- new.env()
> myEnv$a.env <- 1
> myEnv$symbols.env <- "a.env"
>
> a.global <- 2
> symbols.global <- "a.global"
>
> myFun <- function(symbols){do.call("print", lapply(symbols, FUN=as.name))}
>
> do.call("myFun", list(symbols.global)) #prints a.global -- so far, so good.
>
> with(myEnv,
> {
>    do.call("myFun", list(symbols.env))
> })
> # Error in print(a.env) : object 'a.env' not found
>
> # However, attaching myEnv works fine:
> attach(myEnv)
> do.call("myFun", list(symbols.env))
> ##########################
>
> Why is this?
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list