1

考虑以下代码片段(末尾的注释表示行号,不是问题的一部分):

set.seed(1)                                   # 1
my_dat1 <- data.frame(x = runif(100, 1, 10),  # 2
                      w = runif(100, 1, 10))  # 3
                                              # 4
my_dat1$y_orig <- my_dat1$x * my_dat1$w       # 5
my_dat1$y_obs  <- my_dat1$y_orig + rnorm(100) # 6
                                              # 7
my_stat <- function(data, orig_mod) {         # 8
  new_mod <- update(orig_mod, data = data)    # 9
  r <- residuals(new_mod)                     #10
  f <- fitted(new_mod)                        #11
  l <- lowess(f, r)                           #12
  res <- c(l$x, l$y)                          #13
  res                                         #14
}                                             #15

当我lint通过这个文件时, lintr:::addin_lint()我收到以下奇怪的错误:

$ test.R:List of 8
 ..$ filename     : chr "test.R
 ..$ line_number  : int 6
 ..$ column_number: int 14
 ..$ type         : chr "warning"
 ..$ message      : chr "no visible binding for global variable ‘x’"
 ..$ line         : chr "  res <- c(l$x, l$y)                          #13"
 ..$ ranges       :List of 1
 .. ..$ : int [1:2] 14 14
 ..$ linter       : chr "object_usage_linter"
 ..- attr(*, "class")= chr "lint"
$ test.R:List of 8
 ..$ filename     : chr "test.R"
 ..$ line_number  : int 6
 ..$ column_number: int 19
 ..$ type         : chr "warning"
 ..$ message      : chr "no visible binding for global variable ‘y’"
 ..$ line         : chr "  res <- c(l$x, l$y)                          #13"
 ..$ ranges       :List of 1
 .. ..$ : int [1:2] 19 19
 ..$ linter       : chr "object_usage_linter"
 ..- attr(*, "class")= chr "lint"
- attr(*, "class")= chr "lints"

但是,如果我删除#5并且#6我不再有错误:

set.seed(1)                                   # 1
my_dat1 <- data.frame(x = runif(100, 1, 10),  # 2
                      w = runif(100, 1, 10))  # 3
                                              # 4
                                              # 7
my_stat <- function(data, orig_mod) {         # 8
  new_mod <- update(orig_mod, data = data)    # 9
  r <- residuals(new_mod)                     #10
  f <- fitted(new_mod)                        #11
  l <- lowess(f, r)                           #12
  res <- c(l$x, l$y)                          #13
  res                                         #14
}                                             #15

linting 此文件时没有错误。

问题

  1. 这是故意行为还是存在错误lintr
  2. 鉴于lintr抱怨行号中的片段#13而不是行号,为什么行号错误#6
4

0 回答 0