14

I've set up and been regularly updating my R package to GitHub following Hadley's extensive documentation about Devtools, Roxygen2 etc., on my laptop. Yesterday I decided to use my main PC instead and am now looking to push changes up to GitHub. I got the following error after entering document() :

Error: 'Adult_Females' is not an exported object from 'namespace:gbm.auto'

Adult_Females is the name of the first data file in /Data. According to this (scroll down to 'Data')

"files that live in data/ don’t use the usual namespace mechanism and don’t need to be exported."

So... what's a guy to do? I've not edited Adult_Females in any way and the R script I edited doesn't reference it. My suspicion is that this error will pop up for all the data files and it just happened that this is the first of them, but that's conjecture at this point.

Thanks in advance. install_github("SimonDedman/gbm.auto") if you want to have a look.

2020/01/25 edit: looks like I've fixed it. A commit on 26/11/19 saw /Data and all RData files added, with subsequent commit deleting the identical /data folder and files. Not sure if I did that myself, can't think why I would suddenly decide to, but such mysteries are now lost to the sands of time. This change and document() and commit caused the .R files to be removed as export()s from NAMESPACE and the RData files to no longer trigger the titular "data not exported" problem EVEN THOUGH this isn't noted anywhere in DESCRIPTION, NAMESPACE, nor the files themselves. May this weirdness be a lighthouse that warns others of the rock I've spent the last 3 years trapped on!

5
  • 1
    It looks like you are using gbm.auto::Adult_Females in a couple of places. Did you try dropping the gbm.auto::?
    – nrussell
    Commented Nov 1, 2016 at 16:26
  • Hi, thanks. Yes in @examples in gbm.cons.R I have "Adult_Females <- gbm.auto::Adult_Females"; will the data load for users if I don't specify the "::" since data shouldn't be explicitly exported/named? This is defo an area where my understanding of how everything github/roxygen/namespace/description fits together, falls down!
    – dez93_2000
    Commented Nov 1, 2016 at 16:34
  • I'm not positive, but just using Adult_Females should work fine. :: is intended to be used with exported objects specifically, so I believe prepending gbm.auto:: is what is causing the error.
    – nrussell
    Commented Nov 1, 2016 at 16:41
  • hmm. I've just removed all gbm.auto::Adult_Females references in gbm.cons.R and gbm.todoP2.R (a working doc) and no change. Adult_Females only appears within files in those two plus data.R. Could it be that since it's named in data.R as "#' @name Adult_Females" it's expected to be exported to the namespace? That would seem to fly in the face of what Hadley's said in his documentation...
    – dez93_2000
    Commented Nov 1, 2016 at 16:50
  • :: doesn't work for unattached packages and lazy-loaded data.
    – Jack Wasey
    Commented Feb 3, 2019 at 18:36

3 Answers 3

10

I encountered a similar problem when writing an R package which contains a dataset. I guess you must have saved the dataset in a different name.

For example, you may write:

devtools:::use_data(YourDataSetName, pkg = "Path_to_Pkg/data", internal = FALSE)

but in your data.R file, you specified a dataset name at the very end other than YourDataSetName (suppose you followed Hadley's instructions here: http://r-pkgs.had.co.nz/data.html).

Make sure the data object you saved to the "data" folder is the same as you specified in your data.R file.


Note: use_data is now part of usethis package.

1
  • 1
    This must have worked at the time however I've now got the same issue again, out of nowhere. File is /Data/Adult_Females.RData. load() object result name is Adult_Females. /R/data.R object name is "Adult_Females", @name Adult_Females. I have 6 data files sequentially included in data.R; Hadley & you & others all discuss data.R as 1 file but you say there'll be trouble if I specify a different dataset name at the end, implying I should have 6 data.R files?
    – dez93_2000
    Commented Jun 7, 2019 at 0:07
7

for data objects, the names must match in four (4) places, so check them all:

  1. name of data/foo.rda file
  2. name of object in data/foo.rda file
  3. name of R/foo.R file
  4. string at end of R/foo.R file

all four items must match--in this case 'foo'. If you change the name of the foo.rda and foo.R files, say to bar.rda and bar.R, it's easy to forget to rename the object in the .rda file from 'foo' to 'bar.' It's usually easiest to load the file, rename the object, and save the file under the new name:

load('data/foo.rda')
bar <- foo
save(bar, file='data/bar.rda')

If you don't do this, you get an unhelpful error about the object not loaded from namespace. You DON'T need to @export data objects, so instead ensure the names match in all places.

8
  • Thanks. Re: 3 & 4: I don't have my individual data files as .R files in /R folder, but instead as one file, data.R, with details of the 6 datasets in my /data folder. I must have done this based on instructions on Hadley's site somewhere, is it wrong? The naming of that dataset is correct in data.R
    – dez93_2000
    Commented Mar 7, 2018 at 21:20
  • I guess that's fine too... In that case I guess 3 isn't necessary, but I know 1 & 2 are, and I suppose 4 is only necessary for the docs.
    – 0mn1
    Commented Mar 9, 2018 at 0:20
  • Thanks for this 0mn1. Am I missing something? I have this for 'Adult_Females' in my package. 1: /Data/Adult_Females.RData 2: load() object result is named Adult_Females 3&4: /R/data.R object name "Adult_Females", @name Adult_Females. Could it be they now need to be RDa not RData? It used to work fine... Cheers!
    – dez93_2000
    Commented Jun 6, 2019 at 22:14
  • 1
    I believe RData is correct; at least according to the R packages book. Possibly that Data should be data (not capitalized.) Did you generate the package folder with devtools? Doing so would ensure the package structure is correct.
    – 0mn1
    Commented Jun 7, 2019 at 3:13
  • I believe so but it was so long ago that I can't recall. I'll have a check tonight in any case.
    – dez93_2000
    Commented Jun 7, 2019 at 21:36
3

I used save() to create the .rda in the /data folder and added the oxygen headers in the R/data.R file. I was trying all the solutions above but was still getting the error.

What solved it for me was using usethis::use_data(MY_DATA) as suggested here: https://r-pkgs.org/data.html

1
  • Likewise. It's as if use_data is doing something else we are unaware of. Commented Feb 7, 2023 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.