5

我想知道是否有可能在 R 中抑制这些使控制台混乱的输出:

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

这是代码(它是一个简单的 ReferenceClass 来存储 R 项目的配置):

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

到目前为止我尝试过的

我已经尝试过预定义变量的组合和排列,将它们预先设置为 null 等,但 R 仍然顽固地在我的源代码中打印数百个“无可见绑定”注释。

说到 R 的内部结构,有没有人比我更聪明?

更新 1

我试过改成Config <-Config <<-这样就去掉了第二个多余的音符。但是,第一个无关注释仍然存在。

更新 2

我开始灰心了,甚至John Chambers 的示例代码也产生了更多这些可怕的、无关的注释。

更新 3

这些注释出现在 Revolution R v7.0 中,但不在 RStudio 中。似乎 Revolution R v7.0 正在调用R CMD check,通常仅在准备包时使用,因此可以放心地忽略这些注释。

更新 4

Hadley Wickhams 代码也会生成这些注释。显然,可以使用 消除它们utils::globalVariables,但是,这似乎不适用于较新的 ReferenceClasses。即使完全可以使用它们,哈德利说:

globalVariables 是一个可怕的 hack,我永远不会使用它。

4

2 回答 2

5

这个答案的所有功劳归功于@Tyler Rinker。

要消除这些注释,请在上面的源代码前加上以下内容:

# 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"))

另外,如果Revolution R运行了很长时间,有时可能需要重新启动。

于 2014-05-05T16:00:04.830 回答
4

你可以试试这个命令。

编译器::setCompilerOptions(suppressAll = TRUE)

这对我有用,可以抑制消息,例如

注意:全局变量没有可见绑定...
注意:全局函数定义没有可见绑定...

于 2017-06-12T09:37:19.497 回答