Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...