【问题标题】:Why does lintr say "warning: no visible binding for global variable ‘Cloaked’"?为什么 lintr 会说“警告:全局变量‘Cloaked’没有可见绑定”?
【发布时间】:2018-07-09 00:41:26
【问题描述】:

我确定这是我的误解,因为我不是真正的 R 程序员......

我的代码在这里:https://gist.github.com/bnsh/3839c4eb2c6b31e32c39ec312014b2b8

#! /usr/bin/env Rscript

library(R6)

Cloaked <- R6::R6Class("Cloaked",
  public = list(
    msg = function() {
      return(paste(
        "this code _works_, but lintr (https://github.com/jimhester/lintr)",
        "complains that cloak_class.R:19:8: warning: no visible binding for",
        "global variable ‘Cloaked’ when I try to use Cloaked within a",
        "function. It's fine tho, if I use it _outside_ a function."
      ))
    }
  )
)

main <- function() {
  c <- Cloaked$new()
  c$msg()
}

main()

它有效...但是,lintr 抱怨:“cloak_class.R:19:8: 警告:全局变量‘Cloaked’没有可见绑定”

其实这不是一个类,真的,因为这也抱怨:

#! /usr/bin/env Rscript

cloaked <- function() {
  return(paste(
    "this code _works_, but lintr (https://github.com/jimhester/lintr)",
    "complains that cloak_function.R:13:3: warning: no visible global",
    "function definition for ‘cloaked’ when I try to use cloaked within",
    "a function. It's fine tho, if I use it _outside_ a function."
  ))
}

main <- function() {
  cloaked()
}

main()

这段代码也可以运行,但是 lintr 说: cloak_function.R:13:3: 警告:‘cloaked’没有可见的全局函数定义

为什么? 做一些像# nolint start/# nolint end这样的钝器,我能做些什么来满足lintr?

谢谢!

【问题讨论】:

  • 嗨@Hack-R 谢谢!但是,同样的问题,似乎......(我的意思是,代码仍然有效......无论我把你的线路放在哪里,我都会得到相同的 lintr 错误......在库之前,在库之后,在主函数内部,之后main 的定义和对 main 本身的调用之后......)
  • 嗨。我刚刚尝试在此代码上运行 lintr v2,并且不再引发对象使用问题(尽管我可以在同一文件中使用 y &lt;- function() {print(x)} 获得对象使用问题)。所以这看起来像是一个已解决的问题

标签: r lintr


【解决方案1】:

我刚开始使用 lintr 并遇到了同样的问题。这似乎是一个错误。

https://github.com/REditorSupport/atom-ide-r/issues/7 和实际问题 https://github.com/jimhester/lintr/issues/27

目前唯一的解决方法(没有修复 lintr 中的错误)是禁用对象 linter(这并不是那么理想,因为它不会捕获这种形式的真正错误)。例如

with_defaults(object_usage_linter=NULL)

(我不认为对象使用 linter 用于脚本,而是用于包 - 据我所知,它会评估整个脚本(!)以查看定义了哪些全局变量。对于一个包R 文件都是很好的函数定义,但是对于一个脚本,你真的不想在每次 lint 文件时都运行整个脚本)

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 2015-08-02
    • 2011-12-27
    • 2014-08-07
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多