Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Can I use .rds files for my data in a package?

Tags:

package

r

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?

like image 910
JerryN Avatar asked Mar 26 '16 16:03

JerryN


People also ask

How do I read a .RDS file in R?

rds extension. To read a R data file, invoke the readRDS() function. As with a CSV file, you can load a RDS file straight from a website, however, you must first run the file through a decompressor before attempting to load it via readRDS . A built-in decompressor function called gzcon can be used for this purpose.

What is a .RDS file in R?

Rds files store a single R object. According to R documentation: These functions provide the means to save a single R object to a connection (typically a file) and to restore the object, quite possibly under a different name.

What are RData files used for?

rdata or . rda) is a format designed for use with R, a system for statistical computation and related graphics, for storing a complete R workspace or selected "objects" from a workspace in a form that can be loaded back by R.

How do I load data into an R package?

The default R datasets included in the base R distribution Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package.


Video Answer


2 Answers

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.

like image 62
JerryN Avatar answered Oct 15 '22 02:10

JerryN


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") )
    )
like image 34
Rainer Avatar answered Oct 15 '22 01:10

Rainer