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

Gábor Csárdi c@@rd|@g@bor @end|ng |rom gm@||@com
Tue Sep 24 20:12:47 CEST 2019


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 using gmail.com> wrote:
>
> '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
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list