6

As a minimal working example, I'm trying to import some objects from the MASS package into my own package (called Test) - take the abbey dataset for example:

### In R/Test.R:
#' @import MASS
abbey     # Check that the dataset has been imported OK

### DESCRIPTION:
Package: Test
...
Imports: MASS

### NAMESPACE:
# Generated by roxygen2 (4.0.1): do not edit by hand
import(MASS)

I hit Build & Reload in RStudio and get the error:

==> devtools::document(roclets=c('rd', 'collate', 'namespace'))

Updating Test documentation
Loading Test
Error in eval(expr, envir, enclos) : object 'abbey' not found
Writing NAMESPACE
Documentation completed

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Test

* installing to library '.../R/R-3.1.0/library'
* installing *source* package 'Test' ...
** R
** preparing package for lazy loading
Error in eval(expr, envir, enclos) : object 'abbey' not found
Error : unable to load R code in package 'Test'
ERROR: lazy loading failed for package 'Test'
* removing '.../R/R-3.1.0/library/Test'
* restoring previous '.../R/R-3.1.0/library/Test'

Exited with status 1.

It seems that even the most basic import has failed - the system can't find abbey. Clearly I must be overlooking something obvious - what's going wrong?

mchen
  • 9,808
  • 17
  • 72
  • 125
  • It seems to me that `@import MASS` makes only functions from MASS visible on the current search path. You can use `MASS::abbey` to access this variable. There is a quite long section on "Package namespaces" in the official manual "Writing R extensions" - these namespaces things seems to be quite tricky. – Patrick Roocks Aug 13 '14 at 14:34
  • Out of curiousity, have you tried `#' importFrom MASS abbey`? – GSee Aug 13 '14 at 14:55
  • @GSee No - I was just testing if `Imports` would work as expected, and got stuck on that... Will take a look now – mchen Aug 13 '14 at 14:56
  • 1
    @MiloChen As a side note, I have an opinion about importing an entire namespace, which may or may not be shared by others: http://stackoverflow.com/a/18403565 – GSee Aug 13 '14 at 15:11

1 Answers1

0

From what I tried: You can not import abbey because it is not exported by MASS.

> library(MASS)
> 'abbey' %in% getNamespaceExports(getNamespace('MASS'))
[1] FALSE

abbey is a package dataset, defined by a data/abbey.rda file, not a regular symbol name defined by a package.

As suggested you should just use MASS::abbey, or even add in R/data.R:

abbey <- MASS::abbey

to copy the dataset in your package namespace at install time.

Karl Forner
  • 4,175
  • 25
  • 32