簡體   English   中英

將 arguments 傳遞給 callr::r() 后台進程並在其中訪問它們?

[英]Pass arguments to callr::r() background process and access them within?

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()進行調試,但在這種情況下,它不能以通常的方式工作。

問題

如何將 arguments 傳遞給使用callr::r()調用的后台進程,並且在后台進程中訪問那些 arguments?

您必須在 args 列表和 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

多個 arguments 的想法相同:

x <- 3
y <- 4
z <- 5

callr::r(function(x, y, z) { 2 * x * y * z }, args = list(x, y, z))
# [1] 120

來源:這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM