- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包“testing”,数据对象“test_data”保存在文件名为“test_data.RData”的数据文件夹中。
测试包含一个使用此数据对象的函数 hello()
#' 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))
}
require(testing)
testing::hello()
[1] "test_data has 32 rows"
[1] "Hello !"
testing::hello()
Error in nrow(test_data) : object 'test_data' not found
#' Title
#'
#' @export
#' @importFrom testing hello
hello2 <- function(){
hello()
}
require(testingtop)
testingtop::hello2()
Error in nrow(test_data) : object 'test_data' not found
> 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
最佳答案
所以我想我已经从数据函数的文档中找到了解决方案 ?data
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.
> testingtop::hello2()
[1] "test_data has 32 rows"
[1] "Hello !"
关于在另一个包中导入时 R 包数据不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548994/
我是一名优秀的程序员,十分优秀!