4

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? I.e., I do NOT want to simply do a split on "::" and get the second half of the string.

zkurtz
  • 3,230
  • 7
  • 28
  • 64

1 Answers1

6

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")
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245