简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM