Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, is it possible to suppress "Note: no visible binding for global variable"?

I'm wondering if its possible to suppress these outputs in R which are cluttering up the console:

Note: no visible binding for global variable '.->ConfigString' 
Note: no visible binding for '<<-' assignment to 'ConfigString' 

Here is the code (its a simple ReferenceClass to store configuration for an R project):

# Reference Class to store configuration
Config <- setRefClass("Config",
  fields = list(    
    ConfigString = "character"
    ),
    methods = list(
        # Constructor
        initialize = function() {
            ConfigString <<- "Hello, World!"
        }
  )
)

What I have tried so far

I've tried ever combination and permutation of predefining the variables, pre-setting them to null, etc, but R is still stubbornly printing hundreds of "No Visible Binding" notes in my source code.

Is anyone wiser than I when it comes to the internals of R?

Update 1

I've tried changing Config <- to Config <<-, and that gets rid of the second extraneous note. The first extraneous note is still present, however.

Update 2

I'm beginning to lose heart, even sample code by John Chambers generates more of these horrible, extraneous notes.

Update 3

These notes occur in Revolution R v7.0, but don't occur in RStudio. It appears as if Revolution R v7.0 is calling R CMD check, which is normally only used when preparing packages, so these notes can safely be ignored.

Update 4

Hadley Wickhams code also generates these notes. Apparently, it is possible to eliminate them using utils::globalVariables, however, this doesn't seem to work on the newer ReferenceClasses. Even if it were at all possible to use them, Hadley states:

globalVariables is a hideous hack and I will never use it.

like image 387
Contango Avatar asked May 05 '14 14:05

Contango


2 Answers

All credit to @Tyler Rinker for this answer.

To eliminate these notes, prefix the source code above with this:

# Intent:
#   This function suppresses the following notes generated by "R CMD check":
#   - "Note: no visible binding for global variable '.->ConfigString'"
#   - "Note: no visible binding for '<<-' assignment to 'ConfigString'"
# Usage:
#   Add the following right in the beginning of the .r file (before the Reference
#   class is defined in the sourced .r file):
#   suppressBindingNotes(c(".->ConfigString","ConfigString"))
suppressBindingNotes <- function(variablesMentionedInNotes) {
    for(variable in variablesMentionedInNotes) {
        assign(variable,NULL, envir = .GlobalEnv)       
    }
}

suppressBindingNotes(c(".->ConfigString","ConfigString"))

In addition, sometimes Revolution R might need to be restarted if it has been running for a long time.

like image 91
3 revs Avatar answered Oct 08 '22 15:10

3 revs


You can try this command.

compiler::setCompilerOptions(suppressAll = TRUE)

This works for me to suppress the messages like

Note: no visible binding for global variable ...
Note: no visible binding for global function definition ...

like image 36
Sean Huang Avatar answered Oct 08 '22 16:10

Sean Huang