2

This question is slightly different from others on this subject -- I do indeed have a variable called "mypkgdata":

I am writing a package which ships with a data set. This data set is needed for calculations from within the package. In the DESCRIPTION file, I have specified "LazyData" for that purpose, such that the data set is always around when anyone loads the package. When I run the check, however, I get:

.getmodules2: no visible binding for global variable ‘mypkgdata’

What is the correct way of solving this problem?

January
  • 16,320
  • 6
  • 52
  • 74

3 Answers3

4

If you have LazyData: TRUE in your DESCRIPTION file than the following should work:

x <- MyPackageName::mypkgdata
# ... your calculations using x

I get your note also, if I trie calling it without the MyPackageName:: part.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
1

Here is how I have solved it. I create a custom environment in the package, load the data set in this environment, and wrote a function that returns the data set:

pkgEnv <- new.env(parent=emptyenv())

if(!exists("mypkgdata", pkgEnv)) {
  data("mypkgdata", package="mypkg", envir=pkgEnv)
}

getMyPkgData <- function() {
  pkgEnv[["mypkgdata"]]
}

And in the function that utilizes "mypkgdata", I write:

mypkgdata <- getMyPkgData()

Also, I gave up on lazy loading the data, as it is no longer necessary.

January
  • 16,320
  • 6
  • 52
  • 74
0

I think data from a package should not be flagged as invisible. However, a workaround is

if(getRversion() >= "2.15.1")  utils::globalVariables("mypkgdata")

compare https://stackoverflow.com/a/17807914/3805440

Community
  • 1
  • 1
Johannes Ranke
  • 469
  • 3
  • 11