【问题标题】:Use functions of an R package loaded with load_all() in callr::r() and callr::r_bg()在 callr::r() 和 callr::r_bg() 中使用加载了 load_all() 的 R 包的函数
【发布时间】:2022-01-05 08:55:20
【问题描述】:

我正在开发一个 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 的源代码的文件夹。

【问题讨论】:

    标签: r devtools callr


    【解决方案1】:

    我根据 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() 加载的包中的函数hello() 不能在callr::r()callr::r_bg() 中被称为mytest::hello(),而如果在通过@ 安装和加载包后你可以这样做987654330@.

    另一种选择可能是在由 {callr} 启动的新进程中安装新包:

    callr::r(function(){
      devtools::install("./")
      mytest::hello()
    })
    #> [1] "Hello, world!"
    

    请问,是否有其他解决方案可用于使用 load_all() 迭代新包开发并在 {callr} 打开的新 R 会话中使用这些包功能?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多