R包数据在另一个包中导入时不可用 [英] R package data not available when importing in another package

查看:270
本文介绍了R包数据在另一个包中导入时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试"包,数据对象"test_data"保存在数据文件夹中,文件名为"test_data.RData".

I have one package "testing" with a data object "test_data" saved in data folder under file name "test_data.RData".

测试包含一个使用此数据对象的函数hello()

testing contains one function hello() that uses this data object

#' hello
#'
#' @return Prints hello "your_name"
#' @export
#'
#' @examples
#' hello()
hello <- function(your_name = "") {

    print(paste("test_data has", nrow(test_data), "rows"))
    print(sprintf("Hello %s!", your_name))
}

以下代码可以正常工作:

the following code works fine:

require(testing)
testing::hello()
[1] "test_data has 32 rows"
[1] "Hello !"

但这失败了:

testing::hello()
Error in nrow(test_data) : object 'test_data' not found

实际上,我不是直接使用它,而是在另一个导入了此功能的测试包中使用它:

Actually I do not use it directly but in another package testingtop that imports this function:

#' Title
#'
#' @export
#' @importFrom testing hello
hello2 <- function(){

    hello()
}

我在DESCRIPTION的导入"部分中进行了测试,但失败了.

I have testing in the Imports section of DESCRIPTION and this fails.

require(testingtop)
testingtop::hello2()
Error in nrow(test_data) : object 'test_data' not found

如果我将其放在Depends中,则当我使用library()加载程序包时,它将起作用 否则它仍然会失败:

If I put it in Depends it works if I load the package with library() otherwise it still fails:

> library(testingtop)
Loading required package: testing
> testingtop::hello2()
[1] "test_data has 32 rows"
[1] "Hello !"

Restarting R session...

> testingtop::hello2()
Error in nrow(test_data) : object 'test_data' not found

如果它是函数而不是数据对象,则导入会很好,为什么它与数据对象不同,我需要加载导入的包?我错过了什么?它与LazyData和LazyLoad有关吗?

if it was a function instead of a data object Imports would be fine, why is it different with a data object and I need to load the imported package? Did I miss something? And is it related to LazyData and LazyLoad ?

可能是​​此问题的副本

推荐答案

所以我想我已经从数据函数?data

SO I think I've found the solution from the doc of the data function ?data

在没有envir参数的函数中使用数据几乎总是具有将对象放置在用户工作空间中(实际上,替换已存在该名称的任何对象)的不良影响.最好总是通过data(...,envir = environment())将对象放入当前评估环境中.但是,通常最好选择两种方法,这两种方法都在撰写R扩展程序"手册中进行了说明. 对于数据集,请设置一个程序包以使用数据的延迟加载. 对于系统数据对象,例如函数中计算中使用的查找表,请在包源中使用文件"R/sysdata.rda",或在包安装时通过R代码创建对象. 有时一个重要的区别是,第二种方法将对象放置在名称空间中,而第一种则没有. 因此,如果该函数将mytable从包中视为一个对象很重要,则它是系统数据,应使用第二种方法.

Use of data within a function without an envir argument has the almost always undesirable side-effect of putting an object in the user's workspace (and indeed, of replacing any object of that name already there). It would almost always be better to put the object in the current evaluation environment by data(..., envir = environment()). However, two alternatives are usually preferable, both described in the ‘Writing R Extensions’ manual. For sets of data, set up a package to use lazy-loading of data. For objects which are system data, for example lookup tables used in calculations within the function, use a file ‘R/sysdata.rda’ in the package sources or create the objects by R code at package installation time. A sometimes important distinction is that the second approach places objects in the namespace but the first does not. So if it is important that the function sees mytable as an object from the package, it is system data and the second approach should be used.

将数据放入内部数据文件使我的函数hello2()看到

Putting the data in the internal data file made my function hello2() see it

> testingtop::hello2()
[1] "test_data has 32 rows"
[1] "Hello !"

这篇关于R包数据在另一个包中导入时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆