Skip to content
Prev 53943 / 63421 Next

force promises inside lapply

Thanks Bill. I think my confusion may have been in part due to my 
conflating two distinct meanings of the term "evaluate"; the help for 
force says it "forces the evaluation of a function argument" whereas the 
help for eval says it "evaluates the ... argument ... and returns the 
computed value". I found it helpful to compare:

    > lapply(list(a=1,b=2,c=3), function(x){ force(substitute(x)) })
    $a
    X[[i]]

    $b
    X[[i]]

    $c
    X[[i]]

versus

    > lapply(list(a=1,b=2,c=3), function(x){ eval(substitute(x)) })
    Error in eval(substitute(x)) : object 'X' not found

Now for the context my question arose in: given a function

    loader <- function(package, quietly = TRUE) {

        wrapper <- if (quietly) suppressPackageStartupMessages else `{`

        expr <- substitute(wrapper(library(package = package)))

        eval(expr)
    }

prior to R version 3.2, one could do things like

     lapply(c("MASS", "boot"), loader)

but not anymore (which is fine; I agree that one should not depend on 
lapply's implementation details).

Regards,
Ben
On 07/28/2017 06:53 PM, William Dunlap wrote: