0

我正在尝试furrr::future_pmap在 R 中使用来替换purrr::pmap另一个函数中的函数调用。

目前我已经设置了它,所以pmap使用省略号传递其他参数...但是当我尝试使用它执行此操作时,future_pmap我得到未使用的参数错误(参见下面的示例)。我从这里的评论中知道,将省略号参数传递给映射函数 purrr 包、R和其他先前的研究,为了使省略号与 pmap 一起使用,您需要使用function(x,y,z) blah(x,y,z,...)而不是,~blah(..1,..2,..3)但相同的方法似乎不适用于future_map. 做这项工作还有其他秘诀吗?

我创建了一个非常简单的表示,显然我的真实函数在 future_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)
4

1 回答 1

1

好的,我找到了一种工作方式。显然我需要 3 组省略号而不是 1 组。

test_fun_future_pmap <- function(ind, ...){
  
  return( future_pmap(ind, function(x,y,z,...) sumfun(c(x,y,z), ...),...))
  
  
}
于 2020-08-31T04:21:59.560 回答