Marco
Marco

Reputation: 387

How to include raw data in an R package

I'm working on the final assignment of the course Building R Packages.

In this assignment, we need to create an R package based on some example functions provided by the instructors. We need to organize and document the package, then make it available on GitHub. My package is called FARS and is already available in this GitHub repo.

I'm having trouble with making raw data available with the package. After following the instructions provided in the course's readings and also in chapter 14.3 of the book Building R Packages, the files are still not being recognized.

What did I do so far?

  1. Prepared all the package's documentation, including roxygen2 tags, DESCRIPTION, README.Md, and vignette, following these steps in addition to instructions provided in the readings and book mentioned;

  2. Created a subdirectory named inst/extdata in the package's directory;

  3. Copied all three example files (.csv.bz2) with raw data to inst/extdata;

  4. Tested the functions using testthat;

  5. Installed my FARS package.

Now I'm trying to check if one of the files is available after installing the package:

system.file("extdata", "accident_2013.csv.bz2", 
            package = "FARS", 
            mustWork = TRUE)

I get an error message:

Error in system.file("extdata", "accident_2013.csv.bz2", package = "FARS",  : 
  no file found

These data files need to be available with the package, so the examples provided in the vignette work properly.

Upvotes: 2

Views: 1869

Answers (1)

Carl Witthoft
Carl Witthoft

Reputation: 21492

Here's a "real-life" example, using a simple package I wrote recently. I have a "data" directory in the build directory.
EDIT To clarify the comments found in R-exts, the directory tree packagename/inst/extdata is intended for data that your functions call directly, by specifying that directory path. Since you want to load data into your workspace, use the data directory.
My "data" directory contains one file named preciseNumbersAsChar.r . That file contains assignments such as

charE <- {long number string}

If you read the help page for the command data, it explains that files ending in .r are sourced when called.

library(FunWithNumbers)
data('preciseNumbersAsChar')  #works

Which is to say, the defined objects are now in my environment.

It's worth reading the help page for data in detail as different file types are handled slightly differently.

Upvotes: 2

Related Questions