ropensci / drake

An R-focused pipeline toolkit for reproducibility and high-performance computing
https://docs.ropensci.org/drake
GNU General Public License v3.0
1.34k stars 129 forks source link

Arguments to the unlist function get removed #1262

Closed bart1 closed 4 years ago

bart1 commented 4 years ago

I'm not sure if this classifies as a bug or not I just noticed that the transformation of targets removes argument from code. The example below shows that both recursive=F and use.names=F get removed in the combine target. If i do not transform the plan this is not an issue. Is there a way avoid these changes in the code. Maybe the transform could ignore formal arguments of the unlist function?

require(drake)
#> Loading required package: drake
drake_plan(a=target(lapply(2:b, sqrt), transform = map(b=!!(4:6))),
           h=target({
             o<-list(a)
             unlist(o, use.names =  F)
           }, transform = combine(a)))
#> # A tibble: 4 x 2
#>   target command                                          
#>   <chr>  <expr>                                           
#> 1 a_4L   lapply(2:4L, sqrt)                               
#> 2 a_5L   lapply(2:5L, sqrt)                               
#> 3 a_6L   lapply(2:6L, sqrt)                               
#> 4 h      {     o <- list(a_4L, a_5L, a_6L)     unlist(o) }
drake_plan(a=target(lapply(2:b, sqrt), transform = map(b=!!(4:6))),
              h=target({
                o<-list(a)
                unlist(o, recursive = F)
              }, transform = combine(a)))
#> # A tibble: 4 x 2
#>   target command                                          
#>   <chr>  <expr>                                           
#> 1 a_4L   lapply(2:4L, sqrt)                               
#> 2 a_5L   lapply(2:5L, sqrt)                               
#> 3 a_6L   lapply(2:6L, sqrt)                               
#> 4 h      {     o <- list(a_4L, a_5L, a_6L)     unlist(o) }
drake_plan(a=target(lapply(2:b, sqrt), transform = map(b=!!(4:6))),
           h=target({
             o<-list(a)
             unlist(o, recursive = F)
           }, transform = combine(a)), transform=F)
#> # A tibble: 2 x 3
#>   target command                                           transform       
#>   <chr>  <expr>                                            <expr>          
#> 1 a      lapply(2:b, sqrt)                                 map(b = !!(4:6))
#> 2 h      {     o <- list(a)     unlist(o, recursive = F) } combine(a)

Created on 2020-05-26 by the reprex package (v0.3.0)

bart1 commented 4 years ago

A quick work around is to define a small function:

ff<- function(...) unlist(recursive = F,...)
drake_plan(a=target(lapply(2:b, sqrt), transform = map(b=!!(4:6))),
           h=target({
             o<-list(a)
             ff(o)
           }, transform = combine(a)), transform=T)

Alternatively the lists can be concatenated:

p<-drake_plan(a=target(lapply(2:b, sqrt), transform = map(b=!!(4:6))),
              h=target({
                o<-c(a)
              }, transform = combine(a)))

Maybe these are simple enough not really to make an issue out if it?

wlandau commented 4 years ago

This is a strange edge case. It happens because the splicing code calls do.call("c", args, quote = TRUE) on the arguments to your command, and the c() function has formal arguments use.names and recursive.

https://github.com/ropensci/drake/blob/5798e3bd6878760924667c2cf7efd10a6e259e51/R/transform_plan.R#L994

I see no other way to fix this other than to add back those arguments manually after do.call(). should be fixed now.

bart1 commented 4 years ago

Thank you for the quick fix!