我正试图在R中使用furrr::future_pmap
来替换另一个函数中的函数调用中的purrr::pmap
。
目前我已经设置好了,所以pmap
使用省略号...
传递其他参数,但当我尝试使用future_pmap
传递时,会出现未使用的参数错误(见下面的示例(。我从这里的评论中了解到,将省略号参数传递到映射函数purrr包、R和其他先前的研究中,省略号要与pmap一起使用,需要使用function(x,y,z) blah(x,y,z,...)
而不是~blah(..1,..2,..3)
,但同样的方法似乎不适用于future_map
。让这件事成功还有其他秘密吗?
我已经创建了一个非常简单的reprex,很明显,我的真实函数在未来运行更有意义e_pmap
library(purrr)
library(furrr)
#> Loading required package: future
plan(multiprocess)
xd <- list(1, 10, 100)
yd <- list(1, 2, 3)
zd <- list(5, 50, 500)
sumfun <- function(indata, otherdata){
out <- sum(c(indata, otherdata))
return(out)
}
test_fun_pmap_tilde <- function(ind, ...){
return( pmap(ind, ~sumfun(c(..1,..2,..3), ...)))
}
test_fun_pmap <- function(ind, ...){
return( pmap(ind, function(x,y,z) sumfun(c(x,y,z), ...)))
}
test_fun_future_pmap <- function(ind, ...){
return( future_pmap(ind, function(x,y,z) sumfun(c(x,y,z), ...)))
}
#doesn't work as need to use function(x,y,z) instead of tildes
test_fun_pmap_tilde(list(xd, yd, zd), otherdata = c(100,1000))
#> Error in sumfun(c(..1, ..2, ..3), ...): unused arguments (.l[[2]][[i]], .l[[3]][[i]])
#this one works
test_fun_pmap(list(xd, yd, zd), otherdata = c(100,1000))
#> [[1]]
#> [1] 1107
#>
#> [[2]]
#> [1] 1162
#>
#> [[3]]
#> [1] 1703
#but using future_pmap it doesn't work
test_fun_future_pmap(list(xd, yd, zd), otherdata = c(100,1000))
#> Error in (function (x, y, z) : unused argument (otherdata = c(100, 1000))
Created on 2020-08-31 by the reprex package (v0.3.0)
好的,我找到了一种工作方法。显然,我需要3组省略号,而不是1组。
test_fun_future_pmap <- function(ind, ...){
return( future_pmap(ind, function(x,y,z,...) sumfun(c(x,y,z), ...),...))
}