R Yoda
R Yoda

Reputation: 8770

How to call a function using the package and function name in a string with ::

How can I call a function in a package using the fully qualified name ("package::function")?

This does not work:

> eval(call("utils::sessionInfo"))
Error in `utils::sessionInfo`() : 
  could not find function "utils::sessionInfo"

This works:

eval(call("sessionInfo"))

It must be possible to parse and execute a code snippet without internal knowledge, but how?

Upvotes: 0

Views: 126

Answers (1)

bouncyball
bouncyball

Reputation: 10781

Replace call with parse:

eval(parse(text = 'utils::sessionInfo()'))

For example:

eval(parse(text = 'dplyr::count(iris, Species)'))

# A tibble: 3 x 2
  Species        n
  <fctr>     <int>
1 setosa        50
2 versicolor    50
3 virginica     50

Upvotes: 1

Related Questions