Skip to content

[R-pkg-devel] Why doesn't R CMD check warn that globalVariables() is undefined/not imported?

3 messages · Gábor Csárdi, Henrik Bengtsson

#
'R CMD check' will give a warning that 'speed' is an unknown variable in:

  low_speeds <- function(limit) {
    subset(datasets::cars, speed <= limit)
  }

We can tell 'R CMD check' that 'speed' is a false positive by
declaring it a "global" variable.  This can be done, by:

  low_speeds <- function(limit) {
    subset(datasets::cars, speed <= limit)
  }
  utils::globalVariables("speed")

So, far so good. But, why doesn't 'R CMD check' complain about
'globalVariables' not being defined if we use

  globalVariables("speed")

without utils::* and without having an importFrom("utils",
"globalVariables") in the NAMESPACE?

For what it's worth, if I add a globalVariables() inside a function
definition, e.g.

  low_speeds <- function(limit) {
    globalVariables("speed")
    subset(datasets::cars, speed <= limit)
  }
  globalVariables("speed")

then 'R CMD check' will indeed complain about that inner globalVariables():

$ R --vanilla CMD check teeny_0.1.0.tar.gz
* using log directory ?/home/hb/repositories/teeny.Rcheck?
* using R version 3.6.1 (2019-07-05)
* using platform: x86_64-pc-linux-gnu (64-bit)
[...]
* checking R code for possible problems ... NOTE
low_speeds: no visible global function definition for ?globalVariables?
Undefined global functions or variables:
  globalVariables
Consider adding
  importFrom("utils", "globalVariables")
to your NAMESPACE file.

Is this an oversight in R CMD check, or is it implicit that the
'utils' package is attached when installing and checking packages and
we can use 'utils' objects at the top level of a package?

Thanks,

Henrik
#
I believe that this is because if it is outside of a function, then it
runs at INSTALL time, and codetools checks the installed code, i.e.
the function objects typically, and the checks never see the
globalVariables() call.

Gabor

On Tue, Sep 24, 2019 at 7:08 PM Henrik Bengtsson
<henrik.bengtsson at gmail.com> wrote:
#
On Tue, Sep 24, 2019 at 11:13 AM G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:
That would make sense.  Would you say this is a false negative for 'R
CMD check', that is, should it ideally warn about this?

Personally, I argue that one should use:

utils::globalVariables(...)

or

globalVariables(...)
NAMESPACE: importFrom(utils, globalVariables)

/Henrik