0

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
cmo
  • 3,762
  • 4
  • 36
  • 64
  • Something like this [calling methods from other pkgs](https://stackoverflow.com/questions/58452820/referring-to-package-and-function-as-arguments-in-another-function)?, which doesn't solve the problem that the other package has to be around. – Chris Aug 12 '21 at 16:24
  • @cmo: Did the link above solve your problem? If not, what is your ultimate goal? To use an object from a package without relying on `package::objectname`, because this won't pass `R CMD check`? – TimTeaFan Aug 18 '21 at 07:47

0 Answers0