R代码可以在后台进程中运行,如
callr::r(function(){ 2 * 2 })
# [1] 4
当我用args
尝试这一点时,我想不出如何访问它们。我尝试了一些显而易见的事情:
callr::r(function(){ 2 * 2 }, args = list(x=3))
callr::r(function(){ 2 * x }, args = list(x=3))
callr::r(function(){ 2 * args$x }, args = list(x=3))
callr::r(function(){
args <- commandArgs()
2 * args$x
},
args = list(x=3))
# Error: callr subprocess failed: unused argument (x = base::quote(3))
# Type .Last.error.trace to see where the error occurred
我还试图使用browser()
进行调试,但在本例中,它没有按照通常的方式工作。
问题
如何将参数传递给用callr::r()
调用的后台进程,以及如何在后台进程中访问这些参数?
发布于 2022-01-14 07:14:36
您必须将参数移到args列表和so function()中,如下所示:
callr::r(function(x){ 2 * x }, args = list(x = 3))
# [1] 6
或者像这样:
x <- 3
callr::r(function(x){ 2 * x }, args = list(x))
# [1] 6
对于多个参数也有相同的想法:
x <- 3
y <- 4
z <- 5
callr::r(function(x, y, z) { 2 * x * y * z }, args = list(x, y, z))
# [1] 120
来源:这里
https://stackoverflow.com/questions/70707063
复制相似问题