reexporting datasets in r packages

谁说我不能喝 提交于 2020-03-03 04:17:11

问题


In R packages, there is a possibility of reexporting functions. This makes it easy to recycle the same function without having to repeat the code across different packages.

For example, devtools::session_info function is a reexport of sessioninfo::session_info:

#' @export
#' @importFrom sessioninfo session_info
sessioninfo::session_info

I am wondering if a similar thing is also possible for datasets. I have two different packages and I am using the same datasets in both packages. This is not ideal because if I need to change something, I always need to make sure that the change needs to be made in both packages, which increases the likelihood of making an error.

But I don't know how to make this happen. If I do something like:

#' @export
#' @importFrom groupedstats Titanic_full
groupedstats::Titanic_full

I get the following error:

object 'Titanic_full' is not exported by 'namespace:groupedstats'

which makes sense since (https://github.com/IndrajeetPatil/groupedstats/blob/master/NAMESPACE). But this dataset is present in the package:

> vcdExtra::datasets("groupedstats")
Loading package: groupedstats 
             Item      class     dim                                                           Title
1    Titanic_full data.frame  2201x5                                                Titanic dataset.
2 intent_morality data.frame  4016x9               Moral judgments about third-party moral behavior.
3     movies_long data.frame  2433x8 Movie information and user ratings from IMDB.com (long format).
4     movies_wide data.frame 1813x14 Movie information and user ratings from IMDB.com (wide format).

So I'd really appreciate any thoughts on how to achieve such data reexport or if this is impossible in R packages.


回答1:


Data is searched for in a loaded package in a different fashion from the namespace for functions, so it's not technically an export. But you can re-export another package's dataset, which will operate in the same way with one exception: it will not be found using the data() function, which just searches the data/ directory for data objects. The example below:

will work as if it were a "lazy loaded" dataset, e.g. myiris if your package is attached, or using yourpackage::myiris;

will not work with data(myiris, package = "yourpackage").

#' @inherit datasets::iris description source references title
#' @examples
#' # works
#' testdata::myiris
#' # fails
#' data(myiris, package = "yourpackage")
#' @export
myiris <- datasets::iris


来源:https://stackoverflow.com/questions/52690307/reexporting-datasets-in-r-packages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!