Search code examples
rdplyrpackagedata.tabler-package

using data.table in package development: Undefined global functions or variables


The following code in a package I'm developing creates warning in R CMD check

Results[, errorMessage := as.character(errorMessage)]

no visible binding for global variable ‘errorMessage’
   Undefined global functions or variables:
     errorMessage

but Results$errorMessage <- as.character(Results$errorMessage) is fine

Does anyone know why this happens and how should I change the code with data.table to avoid the error?


Solution

  • That it is a known issue, and likely documented (though I just failed to find it in the FAQ ...). One of the magic powers of data.table is to not require quoted identifiers within the scope of the object you use it with (similar to R's own with and within) but that confused the parser checking for global symbols as it sees something unquoted ... and (wrongly) infers a global symbol.

    We cannot fix that parser, but you can silence it. To do so, you can either rely on base R and put a

    utils::globalVariables(c("errorMessage")) 
    

    in any of your R files, or just do

    errorMessage <- NULL
    

    somewhere to create a ('fake' as in unused) identifier.