How to put datasets into an R package How to put datasets into an R package r r

How to put datasets into an R package


I'm not sure if I understood your question correctly. But, if you edit your data in your favorite format and save with

save(myediteddata, file="data.rda")

The data should be loaded exactly the way you saw it in R.

To load all files in data directory you should add

LazyData: true

To your DESCRIPTION file, in your package.

If this don't help you could post one of your files and a print of the format you want, this will help us to help you ;)


In addition to saving as rda files you could also choose to load them as numeric with:

 read.table( ... , colClasses="numeric")

Or as non-factor-text:

 read.table( ..., as.is=TRUE) # which does pretty much the same as stringsAsFactors=FALSE read.table( ..., colClasses="character")

It also appears that the data function would accept these arguments sinc it is documented to be a simple wrapper for read.table(..., header=TRUE).


Preferred saving location of your data depends on its format.

As Hadley suggested:

  • If you want to store binary data and make it available to the user, put it in data/. This is the best place to put example datasets.
  • If you want to store parsed data, but not make it available to the user, put it in R/sysdata.rda. This is the best place to put data that your functions need.
  • If you want to store raw data, put it in inst/extdata.

I suggest you have a look at the linked chapter as it goes into detail about working with data when developing R packages.