本文介绍了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

将数据放入内部数据文件使我的函数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包数据在另一个包中导入时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:57
查看更多