[Rd] no visible binding for global variable for data sets in a package

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 27 16:54:40 CEST 2014


http://cran.r-project.org/doc/manuals/r-patched/R-exts.html#Suggested-packages 
suggests e.g. Lahman::battingLabels, and that does work for lazy-loaded 
datasets (which is what these appear to be).

We have seen a couple of other instances in which this was needed for 
code within the package.  However, in this case I would have thought 
battingLabels etc were really system data and should be in sysdata.rda. 
  See ?data.

As the reference suggests, putting Lahman on the search path does not 
necessarily work correctly: what if a user (or someone else's package!) 
puts another package on the search path which masks battingLabels? 
Assuming you want the version in the package, you have to say so.  (If 
you do not, supply argument 'labels'.)


On 27/08/2014 15:07, Michael Friendly wrote:
> On 8/27/2014 9:29 AM, Michael Friendly wrote:
>>> It works in the sense that  Lahman::Label("yearID")  will
>>> work even when Lahman is not in the search path,
>>> but   R-devel CMD check   will still give the same NOTE,
>>> though you can argue that that note is actally a "false positive".
>> So, this would be version 1 of "2)":
>>
>> Label <- function(var, labels) {
>>      stopifnot(require(Lahman, quietly=TRUE))
>>      if(missing(labels)) labels <- rbind(battingLabels, pitchingLabels,
>> fieldingLabels)
>>      wanted <- which(labels[,1]==var)
>>      if (length(wanted)) labels[wanted[1],2] else var
>> }
>>
>> And this would be version 2, using data():
>>
>> Label <- function(var, labels) {
>>      stopifnot(require(Lahman, quietly=TRUE))
>>      if(missing(labels)) {
>>          data(battingLabels); data(pitchingLabels); data(fieldingLabels)
>>          labels <- rbind(battingLabels, pitchingLabels, fieldingLabels)
>>          }
>>      wanted <- which(labels[,1]==var)
>>      if (length(wanted)) labels[wanted[1],2] else var
>> }
>>
>>
>
> Just to follow up:  R-devel likes this less than it does my initial
> version.  I still get no visible binding NOTES, and complaint about
> using data() in a function:
>
> * checking R code for possible problems ... NOTE
> Label: no visible binding for global variable 'battingLabels'
> Label: no visible binding for global variable 'pitchingLabels'
> Label: no visible binding for global variable 'fieldingLabels'
> battingStats: no visible binding for global variable 'Batting'
> battingStats: no visible global function definition for 'mutate'
> playerInfo: no visible binding for global variable 'Master'
> teamInfo: no visible binding for global variable 'Teams'
>
> Found the following calls to data() loading into the global environment:
> File 'Lahman/R/Label.R':
>    data(battingLabels)
>    data(pitchingLabels)
>    data(fieldingLabels)
> See section 'Good practice' in '?data'.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Emeritus Professor of Applied Statistics, University of Oxford
1 South Parks Road, Oxford OX1 3TG, UK



More information about the R-devel mailing list