R: What causes "In eval(ei, envir) : NAs introduced by coercion"?

Viewed 1442

I sometimes get the following warning in my R code output:

Warning message:
In eval(ei, envir) : NAs introduced by coercion

This happens only when running my R code with R --slave --file=/path/to/sourcecode.R --args arg1 arg2 arg3 arg4 or Rscript /path/to/sourcecode.R arg1 arg2 arg3 arg4 but not when running the code interactively (not even if I run it in an interactive R session on a cluster node).

I cannot provide example code to reproduce the problem because this is a large project with a lot of code spread across several files, and I'm not sure what specific code or circumstances are triggering it.

Googling the error, I found references to an error message referencing "eval(ei, envir)" that happens when you call source from inside of a function . The message I get is different - the post in the link is an error about not being able to find a variable, mine is a warning about NAs being introduced by coercion. So it's probably not the same problem, but I suspect it is somehow still related to using source(), because my code also uses source().

If I knew what "ei" was and why using source() apparently involves calling eval(ei,envir), that might help me figure out what exactly (an environment variable? which one?) has something in it that would trigger an "NAs introduced by coercion" message... does anyone have any ideas what might be going on here?

1 Answers

I have figured out what was causing this. Since Google searches did not find any exact matches for this message, I'm making a Q&A-style post in hopes it will help the next person to Google this particular error.

The warning happens when a variable or other object created in your main code contains values that get coerced to NA in code that you have loaded via source(). Here is some example code to cause it:

test1.R:

print("Starting test1.R")
x <- c('3','bicycle','5')
source('test2.R')
print('Finished test1.R.')

test2.R:

print("Starting test2.R")
y <- as.numeric(x)
print("Finished test2.R")

The output of Rscript test1.R is:

[1] "Starting test1.R"
[1] "Starting test2.R"
[1] "Finished test2.R"
Warning message:
In eval(ei, envir) : NAs introduced by coercion
[1] "Finished test1.R."

Note that even though the line that triggers the error happens in the middle of the code loaded with source(), you don't see the warning in your output until after the code loaded by source() has finished.

You also don't have to be running in batch mode for it to happen. Here I use source() in interactive mode to cause the same message:

> x <- c('3','bicycle','5')
> source('test2.R')
[1] "Starting test2.R"
[1] "Finished test2.R"
Warning message:
In eval(ei, envir) : NAs introduced by coercion

So why was this only happening in batch mode in my original problem? It turned out to be a red herring... when I ran my code in interactive mode, I created a vector to simulate command-line arguments, like this: args <- c('/path/to/infile','27',NA,'0.1'). But when my code was running in batch mode, it created the vector "args" like this: args <- commandArgs(trailingOnly=T). And an NA passed as a command line argument is not a real NA, it is the string 'NA'. So as.numeric('NA') coerces 'NA' to NA. But the argument was only 'NA' instead of NA when running in batch mode.

Those two quirks combined were enough to cause a troubleshooting nightmare that cost me a whole evening. Hopefully this Q&A-style post will save the next person to Google this error message some time!

Related