![](/img/trans.png)
[英]How to use callr::r_bg within a downloadHandler in a Shiny App
[英]Use functions of an R package loaded with load_all() in callr::r() and callr::r_bg()
我正在開發一個 R package 並想在后台運行callr::r()
中的一些功能,或者callr::r_bg()
例如,我創建了一個 package mytest只有一個 function
hello <- function() {
print("Hello, world!")
}
Then loaded the package with pkgload::load_all()
, the function to load a package in development using devtools package. 在此之后,我可以在控制台中運行 function 但不能在后台使用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
另一方面,如果我安裝 package 並運行library(mytest)
,上面的代碼運行沒有問題
callr::r(function(){
mytest::hello()
})
#> [1] "Hello, world!"
請,任何線索為什么callr::r()
找不到 function mytest::hello()
?
看起來load_all()
沒有將路徑添加到可以找到 package mytest的源代碼的文件夾中。
我找到了一個基於調用者 GitHub中的問題的解決方案。
加載 package mytest后,其中只有問題中定義的 function hello()
,使用devtools::load_all()
以下代碼有效
z <- list(mytest::hello)
callr::r(function(z){
z[[1]]()
}, args = list(z))
#> [1] "Hello, world!"
It looks like the function hello()
in the package loaded with load_all()
cannot be referred as mytest::hello()
in callr::r()
or in callr::r_bg()
while you can do so if the package was通過library(mytest)
安裝和加載。
請問,有沒有別的解決辦法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.