Reputation: 11441
Suming two vectors using sum
and its do.call
equivalent would be
sum(1,2)
do.call("sum", list(1,2))
Specifying the sum
function using the double colon operator (base::sum
) will work in the first but fail in the do.call
case.
base::sum(1,2)
do.call("base::sum", list(1,2))
> Error in `base::sum`(1, 2) : could not find function "base::sum"
do.call
work with double colons?Upvotes: 5
Views: 403
Reputation: 253
It is possible to use do.call
to call a double colon operator with package and function that are specified as characters. Since pkgname::fname
is equivalent to `::`(pkgname, fname)
(i.e., the double colon operator in backticks is a function), where pkgname
and fname
can be objects or characters naming objects, we can make `::`
the what
argument of do.call
and pass pkgname
and fname
(as characters) in a list as the args
argument. The arguments of the function named by fname
will then come after the do.call
call. Thus:
do.call(what = `::`, args = list("base", "sum"))(1, 2)
Upvotes: 3
Reputation: 4362
According to the documentation the what
arg of do.call
will take either a function or a non-empty character string naming the function to be called.
If you try implementing the double colon without the quotes it works:
> do.call(base::sum, list(1,2))
[1] 3
So while there is a function named sum
in the package base
, you can't name the function by also specifying the package. Instead, just remove the quotes.
Upvotes: 6