gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:43:24 25 4
gpt4 key购买 nike

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

假设我的数据集(比如 dplyr 数据帧)被写入磁盘为 .rds (或住在当前 session 中)。有没有办法把它放入R无需安装即可打包 rtools和别的?这可以以编程方式完成吗?

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

再次感谢!

最佳答案

我写了一个纯数据包 - 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

关于r - 如何创建仅包含数据集的 R 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com