0

I am trying to use furrr::future_pmap in R to replace purrr::pmap in a function call within another function.

Presently I have it set up so pmap is passing other arguments using the ellipsis ... however when I try and do this using future_pmap I get unused argument errors (see example below). I know from comments in here passing ellipsis arguments to map function purrr package, R and other previous research that for the ellipsis to work with pmap you need to use function(x,y,z) blah(x,y,z,...) instead of ~blah(..1,..2,..3) but the same approach doesn't seem to work for future_map. Is there some other secret to making this work?

I've created a very simple reprex, obviously my real functions make a lot more sense to run in 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)
Sarah
  • 3,022
  • 1
  • 19
  • 40
  • This is usually a warning rather than a fatal error. If that’s the case, I’ve found it’s OK to ignore it. Usually means nothing is coming through the ... – Daniel Lathrop Aug 31 '20 at 03:29

1 Answers1

1

Okay I have found a way for it to work. Apparently I need 3 sets of ellipsis instead of just 1.

test_fun_future_pmap <- function(ind, ...){
  
  return( future_pmap(ind, function(x,y,z,...) sumfun(c(x,y,z), ...),...))
  
  
}
Sarah
  • 3,022
  • 1
  • 19
  • 40