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

Static branching unnames list columns in .data grid #1323

Closed shirdekel closed 4 years ago

shirdekel commented 4 years ago

Prework

Description

I want to pass named lists to targets through the .data grid, but something seems to unname them.

Reproducible example

library(drake)
library(tibble)

arg <- 
  list(
    list(A = "a"),
    list(B = "b")
  )

# Retains list names

drake_plan(
  x = target(
    arg,
    transform = map(
      arg = !!arg,
      .id = FALSE
    )
  )
)
#> # A tibble: 2 x 2
#>   target command      
#>   <chr>  <expr_lst>   
#> 1 x      list(A = "a")
#> 2 x_2    list(B = "b")

my_grid <- 
  tibble(
    arg = arg
  )

# Removes list names

drake_plan(
  x = target(
    arg,
    transform = map(
      .data = !!my_grid,
      .id = FALSE
    )
  )
)
#> # A tibble: 2 x 2
#>   target command   
#>   <chr>  <expr_lst>
#> 1 x      list("a") 
#> 2 x_2    list("b")

Created on 2020-09-22 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.2 (2020-06-22) #> os macOS Mojave 10.14.6 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_AU.UTF-8 #> ctype en_AU.UTF-8 #> tz Australia/Sydney #> date 2020-09-22 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0) #> backports 1.1.9 2020-08-24 [1] CRAN (R 4.0.2) #> base64url 1.4 2018-05-14 [1] CRAN (R 4.0.0) #> callr 3.4.4 2020-09-07 [1] CRAN (R 4.0.2) #> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0) #> devtools 2.3.0 2020-04-10 [1] CRAN (R 4.0.0) #> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.0) #> drake * 7.12.2 2020-06-02 [1] CRAN (R 4.0.0) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.0) #> filelock 1.0.2 2018-10-05 [1] CRAN (R 4.0.0) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.0) #> hms 0.5.3 2020-01-08 [1] CRAN (R 4.0.0) #> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.0) #> igraph 1.2.5 2020-03-19 [1] CRAN (R 4.0.0) #> knitr 1.29 2020-06-23 [1] CRAN (R 4.0.2) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.0) #> pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.1) #> pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.0) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0) #> processx 3.4.4 2020-09-03 [1] CRAN (R 4.0.2) #> progress 1.2.2 2019-05-16 [1] CRAN (R 4.0.0) #> ps 1.3.4 2020-08-11 [1] CRAN (R 4.0.2) #> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.0) #> remotes 2.1.1 2020-02-15 [1] CRAN (R 4.0.0) #> rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.1) #> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0) #> storr 1.2.1 2018-10-18 [1] CRAN (R 4.0.0) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0) #> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.0) #> tibble * 3.0.3 2020-07-10 [1] CRAN (R 4.0.1) #> txtq 0.2.0 2019-10-15 [1] CRAN (R 4.0.0) #> usethis 1.6.1 2020-04-29 [1] CRAN (R 4.0.0) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 4.0.0) #> vctrs 0.3.4 2020-08-29 [1] CRAN (R 4.0.2) #> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.0) #> xfun 0.17 2020-09-09 [1] CRAN (R 4.0.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library ```

Desired result

The .data grid retaining list element names.

wlandau commented 4 years ago

Names should stick in https://github.com/ropensci/drake/commit/9fb42f570bdc64f381710648a971d99a9c5a47ff. But whether you use the CRAN version or the dev version, it's recommended in this case to use language objects than lists.

library(drake)
library(tibble)

grid <- tibble(
  arg = list(
    quote(list(A = "a")),
    quote(list(B = "b"))
  )
)

drake_plan(
  x = target(
    arg,
    transform = map(
      .data = !!grid,
      .id = FALSE
    )
  )
)
#> # A tibble: 2 x 2
#>   target command      
#>   <chr>  <expr_lst>   
#> 1 x      list(A = "a")
#> 2 x_2    list(B = "b")

Created on 2020-09-22 by the reprex package (v0.3.0)

shirdekel commented 4 years ago

Thanks for the quick fix! Can you explain the benefit of quoting/using language objects?

wlandau commented 4 years ago

drake_plan() creates code, and static branching inserts symbols to help automate that code generation. So your first example was really trying to bootstrap back the code that generated the list objects in arg. It’s usually better to begin with code or basic types to avoid having to reconstruct the code that generated the object.