我想知道在我自己的 R 包中添加数据集的简单而简短的过程。我试图关注https://www.youtube.com/watch?v=Bj0gHafa4GE但之后我不知道为什么数据没有出现在 R 中。因为包成功上传到 CRAN 中,名为 "DEEVD"
。如果我写
> data(Rhone)
Warning message:
In data(Rhone) : data set ‘Rhone’ not found
我不知道我在哪里错过了连接,因为在 R-CMD 检查期间没有错误,并且数据帮助文件运行良好。 如果需要任何进一步的细节,请指导我并在评论部分写下。 编辑: @Mossa 的答案非常有用,但它在 Rstudio 中工作正常。当我在 CRAN 中更新我的包时,也出现了同样的问题。下载后即可查看DEEVD
最佳答案
好吧,我终于可以回答了。
问题在于您的 src/Rhone.R
文件中写入的内容。
在这里,您将重新定义Rhone
。这不是该文件应该包含的内容。
相反,此文件 src/Rhone.R
应该包含数据集 Rhone
的文档。
#' Documentation for `Rhone` should reside here.
#'
#'
"Rhone"
现在,这就是我解决这个问题的方法。
首先,在 Rstudio 中将包作为项目打开。
然后(如果您还没有)通过 install.packages("usethis")
安装 {usethis}
包。
然后运行usethis::use_data_raw("Rhone")
。您可以阅读以下文档
这个函数,了解它的作用:
> usethis::use_data_raw("Rhone")
√ Setting active project to
[REDACTED]
√ Creating 'data-raw/'
√ Adding '^data-raw$' to '.Rbuildignore'
√ Writing 'data-raw/Rhone.R'
* Modify 'data-raw/Rhone.R'
* Finish the data preparation script in 'data-raw/Rhone.R'
* Use `usethis::use_data()` to add prepared data to package
现在,转到 data-raw/Rhone.R
文件,并将“Rhone”的定义放入其中。
最后,data-raw/Rhone.R
应如下所示:
## code to prepare `Rhone` dataset goes here
Rhone <-
c(
1355,
1492,
1692,
1766,
1903,
2040,
2177,
2314,
2451,
2588,
2725,
2862,
2999,
3136,
3273,
3410,
3547,
3686,
3822,
3959,
4096,
4233,
4370
)
usethis::use_data(Rhone, overwrite = TRUE)
运行/获取该脚本。现在,这会将名为 Rhone.rda
的文件保存到 data/
文件夹中。
现在,当您构建包时,此数据文件是唯一的文件
与Rhone
这个名字有关。而不是像以前那样重新定义它。
如果更改 Rhone
的定义,则必须重新运行/重新获取 data-raw/Rhone.R
。但只有在更改 Rhone
时才需要这样做。并且您只能在 data-raw/Rhone.R
中更改 Rhone
。没有其他地方可以改变Rhone
。
如果您构建并加载包,那么您之前使用的代码将起作用。
> library(DEEVD)
> data(Rhone)
> Rhone
[1] 1355 1492 1692 1766 1903 2040 2177 2314 2451 2588
[11] 2725 2862 2999 3136 3273 3410 3547 3686 3822 3959
[21] 4096 4233 4370
关于r - 在R包中添加数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69698805/