如何创建仅包含数据集的R包?

时间:2018-05-22 18:20:49

标签: r

我正在寻找创建只需要包含单个数据集的R包的最简单方法。

假设我的数据集(例如,dplyr数据帧)作为.rds写入磁盘(或存在于当前会话中)。有没有办法将其放入R包而无需安装rtools和其他人?这可以通过编程方式完成吗?

我的想法是每次运行其他代码时都会重新创建这个包。然后我可以将这个包数据集发送到其他程序。

再次感谢!

1 个答案:

答案 0 :(得分:8)

我写了一个仅限数据的包 - https://github.com/nfultz/ec2instances.info/blob/master/R/ec2instances.R

在.onLoad函数中,您可以通过查询网站加载您喜欢的数据。

或者,您可以使用package.skeleton制作一个空包。您应该编辑帮助文件,但我只使用了sed:

> 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)

检查它是否有效:

> 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