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

2

5

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.

Warlord answered 5/5, 2014 at 14:43 Comment(5)
Have you seen this?Opuntia
If you had read what I linked to, you wouldn't have referred to these as "horrible, extraneous errors". Horrible, maybe. Extraneous, for sure. Errors? Not even close.Opuntia
@Opuntia You're right, I've changed "error" to "note".Warlord
Adding something like ConfigString <- NULL right in the beginning of the function (before you use these variables) also makes the notes go away.Whitebeam
@Tyler Rinker You're right, this fixed the issue. I've added an answer below, I wisht here was some way to give your answer 10 votes!Warlord
W
4

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.

Warlord answered 5/5, 2014 at 14:44 Comment(2)
Is this less "hideous" than using utils::globalVariables?Villanelle
I believe this is worse, because those objects will be accessible with pkg::: while variables used in globalVariables() won't, so this solution will create a bit of user facing mess. I'm respectfully downvoting this.Excitant
M
4

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 ...

Mellon answered 12/6, 2017 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.