Home > Software design >  How to solve- Error: (converted from warning)
How to solve- Error: (converted from warning)

Time:06-27

I have been getting this error for the first time for commands that used to run well before:

# conversion from char to numeric: 
  as.numeric(df$col) ->  df$col
 
 Error: (converted from warning) NAs introduced by coercion

# running metafor

  rma(yi, vi, data=r1s2) 
  Error: (converted from warning) Studies with NAs omitted from model fitting.

The issue must be with the R environment as these commands are running perfectly on a different computer. The only wrong I can think of is installing a package from GitHub or updating R a few hours ago. The only relevant answer I've found so far is also not working:

Sys.setenv(R_REMOTES_NO_ERRORS_FROM_WARNINGS="true")

CodePudding user response:

This seems to be a warning level issue. If the warning level is 2, warnings become errors. From the documentation, my emphasis.

warn:
integer value to set the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two (or larger, coercible to integer), all warnings are turned into errors.

old_ops <- options(warn = 2)

warning("this is a warning")
#> Error in eval(expr, envir, enclos): (converted from warning) this is a warning

x <- "a"
as.numeric(x)
#> Error in eval(expr, envir, enclos): (converted from warning) NAs introduced by coercion

options(old_ops)

Created on 2022-06-25 by the reprex package (v2.0.1)


If you say that

The issue must be with the R environment as these commands are running perfectly on a different computer.

then check if you have a file named .RData in your R startup directory. If you have one, then you probably set the warning level in a previous session and now it being restored every time you run R. Delete this file and this behavior will go away.
See also this SO post.

  •  Tags:  
  • r
  • Related