0

I'm building my own R package and I have a reference data frame that needs to be accessed by the program. It's not very large and would require a user supplied string to search for the appropriate data.

Essentially, I have an .Rdata file with 1 data frame. I have stored the file in the /Rpackage_name/data/ directory of the package folder.

I would like for the package to load the data OR have access to its contents. This is what is confusing me.

What am I doing wrong?

GenericPackageName <- function () {
    #data("GenericPackageName")    did not work
}
IMPERATOR
  • 277
  • 1
  • 3
  • 14
  • If it's not very large, why not just make it into a `.txt` instead and move on? You could also just assign it a name and put it into a `.R` file, and it will be loaded when the package is attached. – Rich Scriven Jul 24 '14 at 00:49
  • If it's in your package and in the data directory there really shouldn't be any issues. Take a look at this question: http://stackoverflow.com/questions/10891146/load-data-set-automatically – Dason Jul 24 '14 at 01:09
  • If I'm reading your code above correctly, it may not work because the function name and the data name are the same thing. Would that be consistent with the error msg you got? – Bryan Hanson Jul 24 '14 at 01:23
  • could be several issues. read the [section on including data in an r package on cran](http://cran.r-project.org/doc/manuals/R-exts.html#Data-in-packages) – rawr Jul 24 '14 at 01:45

2 Answers2

0

If it is really not large, then put it in the code. Here is how to do it. Load the data into R, then use the sink() function to redirect the output to a file, and the dput() function to create the R code that creates the object. Assuming your data set is called iris, here is how to do it:

data(iris)
sink("/tmp/iris.R")
dput(iris)
sink(NULL)

system("cat /tmp/iris.R")

# structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
# 5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 
# ...
# 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
# 3L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length", 
# "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = c(NA, 
# -150L), class = "data.frame")

Then just include this code in your package:

mydata <- structure(list(Sepal.length = ...
Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
0

You can dump it into an R file and put that file in the R folder. When the package is added to the search list, the file will be sourced and the objects created.

Here's an example

## create some data in a fresh R session
> ls()
# character(0)
> d <- data.frame(x = 1:5, y = letters[1:5])
> save.image()
## load the data into R
> load(".RData", verbose = TRUE)
#Loading objects:
#  d
#  .Random.seed
## dump it into "newData.R" then source it
> dump("d", "newData.R")
> source("newData.R")
> ls()
# "d"

Here's a look at what dump does

> cat(readLines("newData.R"))
# d <- structure(list(x = 1:5, y = structure(1:5, .Label = c("a", "b",  
#     "c", "d", "e"), class = "factor")), .Names = c("x", "y"), 
#     row.names = c(NA,  -5L), class = "data.frame")
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245