简体   繁体   中英

Get a function from a string of the form “package::function”

There has been discussion on how to get a variable from a string. Indeed, get works for, say, the data.table function: get("data.table") returns data.table . However,

> get("data.table::data.table")
Error in get("data.table::data.table") : 
  object 'data.table::data.table' not found

Is there a way to do this that preserves the reference to the package name? Ie, I do NOT want to simply do a split on "::" and get the second half of the string.

You could just use the envir argument to get the function from the namespace.

get("data.table", envir = getNamespace("data.table"))

Or more simply as @joran notes, getFromNamespace() can be used.

getFromNamespace("data.table", "data.table")

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