简体   繁体   English

从“package :: function”形式的字符串中获取函数

[英]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 . 实际上, get使用data.table函数: get("data.table")返回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. 您可以使用envir参数从命名空间中获取函数。

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

Or more simply as @joran notes, getFromNamespace() can be used. 或者更简单地说@joran注释,可以使用getFromNamespace()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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