I've finally figured out the answer to this question (after several years). All comments and answers suggested adding data.table
to Depends
or Imports
, but this is incorrect; the package does not depend on data.table
and, that could be any package hypothetically, not just data.table, meaning taken to logical conclusion, the suggestion would require adding all possible packages to Depends
-- since that dependency is provided by the user providing the instruction
, not by the function provided by the package.
Instead, basically, it's because call to eval
is done within the namespace of the package, and this does not include the functions provided by other packages. I ultimately solved this by specifying the global environment in the eval
call:
myFunc = function(instruction) {
eval(parse(text=instruction), envir=globalenv())
}
Why this works
This causes the eval
function to be done in the environment that will include the requisite packages in the search path.
In the data.table
case it's particularly hard to debug because of the complexity of the function overloading. In this case, the culprit is not actually the :=
function, but the [
function. The :=
error is a red herring. At the time of writing, the :=
function in data.table
is defined like this:
https://github.com/Rdatatable/data.table/blob/348c0c7fdb4987aa6da99fc989431d8837877ce4/R/data.table.R#L2561
":=" <- function(...) stop('Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").')
That's it. What that means: any call to :=
as a function is stopped with an error message, because this is not how the authors intend :=
to be used. Instead, :=
is really just keyword that's interpreted by the [
function in data.table
.
But what happens here: if the [
function isn't correctly mapped to the version specified by data.table
, and instead is mapped to the base [
, then we have a problem -- since it can't handle :=
and so it's getting treated as a function and triggering the error message. So the culprit function is [.data.table
-- the overloaded bracket operator.
What's happening is in my new package (that holds myFuncInPackage
), when it goes to evaluate the code, it resolves the [
function to the base [
function instead of to data.table
's [
function. It tries to evaluate :=
as a function, which is not being consumed by the [
since it's not the correct [
, so :=
is getting passed as a function instead of as a value to data.table
's, because data.table
is not in the namespace (or is lower in the search()
hierarchy. In this setting, :=
is not understood and so it's being evaluated as a function, thus triggering the error message in the data.table
code above.
When you specify the eval to happen in the global environment, it correctly resolves the [
function to [.data.table
, and the :=
is interpreted correctly.
Incidentally, you can also use this if you're passing not a character string but a code block (better) to eval()
inside a package:
eval(substitute(instruction), envir=globalenv())
Here, substitute
prevents the instruction
from being parsed (incorrectly) within the package namespace at the argument-eval stage, so that it makes it intact back to the globalenv where it can be correctly evaluated with the required functions in place.
eval(parse())
is discouraged from. – SemiweeklymyFunc
-- no data.table anything. But it can't be used with data.table without adding it to Depends... – Wizeneval(parse())
is discouraged, and this is a pointless example, but the question still stands...in some cases I can't get around it. – Wizeneval(parse(text=instruction))
whereinstruction
can be anything! At the time of evaluation any function required byinstruction
must be available; this should be specified in the usage instructions for your package. You're seeing this wheninstruction
requires a function indata.table
; load 'data.table' before executingmyFuncInPackage(instruction)
and see if it works. – Punchy:=
operator that you use in your function is defined withindata.table
package, so yes, your package does depend ondata.table
– Hanker:=
in my function. That was passed by the user, in the "instruction" variable. it has nothing to do with the package... – WizenNAMESPACE
file toimport(data.table)
andDESCRIPTION
toImports: data.table
? I got the same problem recently just because missing entry inNAMESPACE
file. – Spectrometer