8

I want to do.call an (exported) function from a package and need to specify the package and function in the what argument as a character string (i.e., when the package is not loaded into my environment or there is a potential function name conflict).

The function name is a character string like "lme4::lmer" which specifies both the package and the function.

For example, to conditionally call a function (similar to this question):

FUN = if(TRUE) {
  "lme4::lmer"
} else {
  "nlme::nmle"
}

args.list = list(Reaction ~ Days + (Days | Subject), 
                 quote(lme4::sleepstudy))

do.call(what=FUN, args=args.list)
# Error in `lme4::lmer`(Reaction ~ Days + (Days | Subject), lme4::sleepstudy) : 
#   could not find function "lme4::lmer"

Other ways work but are not what I need:

# Load the package and use only the function string 
library(lme4)
do.call("lmer", ...)

# Or calling the function directly: 
lme4::lmer(...)
Brian D
  • 2,570
  • 1
  • 24
  • 43
Jack Tanner
  • 934
  • 1
  • 8
  • 24

2 Answers2

12

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 {
        x
    }
}
getfun("lme4::lmer")

And this should work

do.call(getfun("lme4::lmer"), list(
    Reaction ~ Days + (Days | Subject), 
    quote(lme4::sleepstudy)))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    I've never used such arcana as these `get*`s, but `getFromNamespace` was what Uwe Ligges recommended a decade ago: http://r.789695.n4.nabble.com/do-call-method-within-namespace-td797206.html – Frank Aug 16 '16 at 21:03
  • 1
    @Frank I just looked at the source of `::` and did what it did. I'm sure that would work as well. – MrFlick Aug 16 '16 at 21:04
  • 1
    Ah ok, I think your way makes more sense. Looks like `getFromNamespace` is more like `:::` (also grabbing unexported funs). – Frank Aug 16 '16 at 21:06
4
do.call(eval(parse(text="lme4::lmer")), ...)

eval with parse will work!

> tmp <- expand.grid(letters[1:2], 1:3, c("+", "-"))
> do.call(eval(parse(text='stringr::str_c')), c(tmp, sep = ""))

 [1] "a1+" "b1+" "a2+" "b2+" "a3+" "b3+" "a1-" "b1-" "a2-" "b2-" "a3-" "b3-"

Reference: https://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/

amrrs
  • 6,215
  • 2
  • 18
  • 27