Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create an R package that only contains a dataset?

Tags:

r

I am looking for the easiest way to create an R package that only needs to contain a single dataset.

Assume my dataset (say, a dplyr dataframe) is written to disk as .rds (or lives in the current session). Is there a way to put it into an R package without having to install rtools and others? Can this be done programmatically?

The idea is that this package would be re-created every time I run some other code. Then I could ship this package-dataset to other programs.

Thanks again!

like image 923
ℕʘʘḆḽḘ Avatar asked May 22 '18 18:05

ℕʘʘḆḽḘ


1 Answers

I wrote a data-only package - https://github.com/nfultz/ec2instances.info/blob/master/R/ec2instances.R

In the .onLoad function, you can load data however you like, in my case from querying a website.

Alternatively, you could just use package.skeleton to make a empty package. You are supposed to edit the help files, but I just used sed instead:

> foo <- iris
> package.skeleton("irispkg", "foo")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './irispkg/Read-and-delete-me'.
> system("sed -i 's/^%%//' irispkg/man/foo.Rd")
> system("R CMD build irispkg")
* checking for file 'irispkg/DESCRIPTION' ... OK
* preparing 'irispkg':
* checking DESCRIPTION meta-information ... OK
* installing the package to process help pages
* saving partial Rd database
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* looking to see if a 'data/datalist' file should be added
* building 'irispkg_1.0.tar.gz'

> install.packages("./irispkg_1.0.tar.gz", repos=NULL)
Installing package into '/home/neal/R/x86_64-pc-linux-gnu-library/3.4'
(as 'lib' is unspecified)
* installing *source* package 'irispkg' ...
** data
** help
Warning: /tmp/RtmpW8T900/Rbuild8c0136c3b349/irispkg/man/irispkg-package.Rd:27: All text must be in a section
Warning: /tmp/RtmpW8T900/Rbuild8c0136c3b349/irispkg/man/irispkg-package.Rd:28: All text must be in a section
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (irispkg)

check that it works:

> require(irispkg)
Loading required package: irispkg
> data(foo)
> head(foo)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
like image 77
Neal Fultz Avatar answered Oct 08 '22 23:10

Neal Fultz