10

I'm trying to convert some code into a package. According to the documentation, only .RData files should be in the data directory, but I'd rather use .rds files because they don't retain the file name. There are times when I save with a different name than I want to use when reading in later. And I really only want to have one data set for file, so the ability of .RData files to store more is actually a negative.

So my question is why not allow .rds files in the package data directory? Or is there another way to solve this problem?

JerryN
  • 2,356
  • 1
  • 15
  • 49
  • Hadley has some good documentation [here](http://r-pkgs.had.co.nz/data.html). – r2evans Mar 26 '16 at 23:29
  • The answer to my question seems to be that the only acceptable data files in /data are those saved with 'save', which means they are in the .RData format. The Hadley link that @r2evans points to says this. As does the section 1.1.6 that rawr points to. – JerryN Mar 27 '16 at 02:53
  • Are there some specific reasons why using RData instead of RDS? as you say @JerryN RDS are very handy to import data in script without any a priori knowledge on the object name. Just curious to know – Simon C. Feb 02 '23 at 18:39

2 Answers2

5

The only acceptable data files in /data are those saved with 'save', which means they are in the .RData format. Hadley's link, which @r2evans points to, says this. As does section 1.1.6, which @rawr points to.

Thomas
  • 43,637
  • 12
  • 109
  • 140
JerryN
  • 2,356
  • 1
  • 15
  • 49
2

Old question - but you can. It is a two step process.

  1. save your data as .rds file
  2. create an R file in the data directory which loads the rds data.

I am doing this as followed:

rdsFile <- paste0(schemeName, "_example.rds")
saveRDS(
      dmdScheme_example,
      file = here::here( "data", rdsFile )
    )
cat(
      paste0(schemeName, "_example <- readRDS(\"./", rdsFile, "\")"),
      file = here::here( "data", paste0(schemeName, "_example.R") )
    )
Rainer
  • 8,347
  • 1
  • 23
  • 28
  • 1
    One problem - CRAN does not like it. So it works for internal packages, but not CRAN. I posted a follow-up question there https://stackoverflow.com/questions/56293910/create-r-data-with-a-dynamic-variable-name-from-function-for-package.\ – Rainer May 24 '19 at 13:43