Jack Tanner Jack Tanner - 1 year ago 91
R Question

do.call a function in R without loading the package

I want to

an (exported) function from a package. The function name is a character string like

# Works, but not what I need
do.call("lmer", ...)
# Also works, but not what I need
# Doesn't work:
do.call("lme4::lmer", ...)

Answer Source

You can't include :: in the string because :: is a function. That string isn't evaluated and parsed. do.call just assumes a function by that name exists. Just like ::, $ is also a function so this doesn't work either

a<-list(f=function(x) x+1)
do.call(a$f, list(4))
# [1] 5
do.call("a$f", list(4))
# Error in do.call("a$f", list(4)) : could not find function "a$f"

If you want to be able to find functions from namespaces, you can write a helper function that parses values when they contain ::

getfun<-function(x) {
    if(length(grep("::", x))>0) {
        parts<-strsplit(x, "::")[[1]]
        getExportedValue(parts[1], parts[2])
    } else {

And this should work

do.call(getfun("lme4::lmer"), list(
    Reaction ~ Days + (Days | Subject), 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download