我正在开发一个R包,并希望在后台使用callr::r()或callr::r_bg()运行正在开发的包中的一些函数。
例如,我创建了一个只有一个函数的包mytest。
hello <- function() {
print("Hello, world!")
}然后用pkgload::load_all()加载包,该函数使用devtools包加载开发中的包。之后,我可以在控制台中运行函数,但不能使用callr::r()在后台运行该函数。
callr::r(function(){
mytest::hello()
})
#> Error: callr subprocess failed: there is no package called 'mytest'
#> Type .Last.error.trace to see where the error occurred另一方面,如果我安装了软件包并运行了library(mytest),上面的代码就会顺利运行。
callr::r(function(){
mytest::hello()
})
#> [1] "Hello, world!"请告诉我为什么callr::r()找不到函数mytest::hello()
看起来load_all()并没有将路径添加到可以找到包mytest源代码的文件夹中。
发布于 2021-11-29 18:11:29
我根据callr GitHub中的一个问题找到了一个解决方案。
加载包mytest之后,该包中只包含问题中定义的函数hello(),使用devtools::load_all(),下面的代码可以工作
z <- list(mytest::hello)
callr::r(function(z){
z[[1]]()
}, args = list(z))
#> [1] "Hello, world!"看起来,在带有load_all()的包中,函数load_all()在callr::r()中或在callr::r_bg()中不能被称为mytest::hello(),而如果包是在通过library(mytest)之后安装和加载的,则可以这样做。
另一个选项可能是在{callr}启动的新进程中安装新包:
callr::r(function(){
devtools::install("./")
mytest::hello()
})
#> [1] "Hello, world!"请注意,是否有另一种解决方案可用于使用load_all()迭代新包开发并在{callr}打开的新R会话中使用这些包函数?
https://stackoverflow.com/questions/70138924
复制相似问题