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

前提是你 提交于 2019-11-27 06:18:17

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/23475309/in-r-is-it-possible-to-suppress-note-no-visible-binding-for-global-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!