r-lib / rcmdcheck

Run R CMD check from R and collect the results
https://rcmdcheck.r-lib.org
Other
115 stars 27 forks source link

devtools::check() incorrectly recognises within() components as global variables, therefore creating false positive note. #203

Closed GMSL1 closed 1 year ago

GMSL1 commented 1 year ago

If a function uses the built-in R function within(), such as the reproducible example below:

testfunc <- function(x) {
  testdf <- data.frame(
    x = x,
    isEven = (x %% 2 == 0)
  )
  testdf <- within(
    testdf,
    {
      Type = "Unknown"
      Type[isEven] = "Even"
      Type[!isEven] = "Odd"
    }
  )
  return(testdf)
}

which, as expected, returns the following:

> testfunc(1:6)
  x isEven Type
1 1  FALSE  Odd
2 2   TRUE Even
3 3  FALSE  Odd
4 4   TRUE Even
5 5  FALSE  Odd
6 6   TRUE Even

However, if one calls devtools::check() on this, one gets the note:

checking R code for possible problems ... NOTE
  testfunc: no visible binding for global variable 'isEven'
  Undefined global functions or variables:
    isEven

So R CMD Check is assuming that isEven in Type[isEven] = "Even" is a global variable when it is not, thus generating an incorrect note.

gaborcsardi commented 1 year ago

As you say, this is not rcmdcheck, but R CMD check, so you'll need to report it to base R.