I am writing an R package and I would like to avoid explicit dependency on a particular package (otherwise my package would have an unwieldy number of package dependencies).
rlang::call2()
is widely used to call functions from another R package without triggering a dependency on that other package. This strategy is used e.g. in parsnip
from the tidymodels
framework.
However, I am interested in retrieving a variable from a package, not a function.
How can I use rlang::call2()
to retrieve a variable from a namespace?
For a function bar()
from package foo
, I can use rlang::call2()
in the standard way, like this:
call <- rlang::call2(.fn="bar",.ns="foo")
result <- base::eval(call)
But suppose that package foo
has a variable named a
which is just a character vector. The same setup no longer works:
call <- rlang::call2(.fn="a",.ns="foo")
result <- base::eval(call)
# Error in base::eval(call) : attempt to apply non-function
For example:
- package =
phyloseq
- variable =
distanceMethodList
call <- rlang::call2(.fn="distanceMethodList",.ns="phyloseq")
result <- base::eval(call)
# Error in base::eval(call) : attempt to apply non-function