Drake: rmarkdown::render with transform

Created on 13 May 2019  Â·  3Comments  Â·  Source: ropensci/drake

Is it possible to apply transform to rmarkdown::render? Something like below for example:

planMain <- drake_plan(
  sReportModel = target(
    rmarkdown::render(...),
    transform = cross(prevTarget)
  )
)

My scenario is that each of the previous crossed targets has enough information to render its own report, preferably named after its own target's name.
I will have a summary report as well at the end, but in this case a drilled-down report for each target would be useful too.

question

Most helpful comment

.id_chr in map() and cross() makes this easier.

library(drake)
plan <- drake_plan(
  y = target(
    f(x),
    transform = cross(f = c(a, b), x = c(1, 2))
  ),
  report = target(
    rmarkdown::render(
      knitr_in(!!paste0(.id_chr, ".Rmd")),
      file_out(!!paste0(.id_chr, ".html"))
    ),
    transform = map(y)
  )
) 

print(plan)
#> # A tibble: 8 x 2
#>   target      command                                                      
#>   <chr>       <expr>                                                       
#> 1 y_a_1       a(1)                                                        …
#> 2 y_b_1       b(1)                                                        …
#> 3 y_a_2       a(2)                                                        …
#> 4 y_b_2       b(2)                                                        …
#> 5 report_y_a… rmarkdown::render(knitr_in("report_y_a_1.Rmd"), file_out("re…
#> 6 report_y_b… rmarkdown::render(knitr_in("report_y_b_1.Rmd"), file_out("re…
#> 7 report_y_a… rmarkdown::render(knitr_in("report_y_a_2.Rmd"), file_out("re…
#> 8 report_y_b… rmarkdown::render(knitr_in("report_y_b_2.Rmd"), file_out("re…

# Just for the sake of the reprex:
files <- paste0(plan$target[5:8], ".Rmd")
tmp <- file.create(files)

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-05-13 by the reprex package (v0.2.1)

Alternatively, you can use deparse(substitute()).

library(drake)
plan <- drake_plan(
  y = target(
    f(x),
    transform = cross(f = c(a, b), x = c(1, 2))
  ),
  report = target(
    rmarkdown::render(
      knitr_in(!!paste0(deparse(substitute(y)), ".Rmd")),
      file_out(!!paste0(deparse(substitute(y)), ".html"))
    ),
    transform = map(y)
  )
)  

print(plan)
#> # A tibble: 8 x 2
#>   target       command                                                     
#>   <chr>        <expr>                                                      
#> 1 y_a_1        a(1)                                                       …
#> 2 y_b_1        b(1)                                                       …
#> 3 y_a_2        a(2)                                                       …
#> 4 y_b_2        b(2)                                                       …
#> 5 report_y_a_1 rmarkdown::render(knitr_in("y_a_1.Rmd"), file_out("y_a_1.ht…
#> 6 report_y_b_1 rmarkdown::render(knitr_in("y_b_1.Rmd"), file_out("y_b_1.ht…
#> 7 report_y_a_2 rmarkdown::render(knitr_in("y_a_2.Rmd"), file_out("y_a_2.ht…
#> 8 report_y_b_2 rmarkdown::render(knitr_in("y_b_2.Rmd"), file_out("y_b_2.ht…

# Just for the sake of the reprex:
files <- paste0(plan$target[1:4], ".Rmd")
tmp <- file.create(files)

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-05-13 by the reprex package (v0.2.1)

All 3 comments

.id_chr in map() and cross() makes this easier.

library(drake)
plan <- drake_plan(
  y = target(
    f(x),
    transform = cross(f = c(a, b), x = c(1, 2))
  ),
  report = target(
    rmarkdown::render(
      knitr_in(!!paste0(.id_chr, ".Rmd")),
      file_out(!!paste0(.id_chr, ".html"))
    ),
    transform = map(y)
  )
) 

print(plan)
#> # A tibble: 8 x 2
#>   target      command                                                      
#>   <chr>       <expr>                                                       
#> 1 y_a_1       a(1)                                                        …
#> 2 y_b_1       b(1)                                                        …
#> 3 y_a_2       a(2)                                                        …
#> 4 y_b_2       b(2)                                                        …
#> 5 report_y_a… rmarkdown::render(knitr_in("report_y_a_1.Rmd"), file_out("re…
#> 6 report_y_b… rmarkdown::render(knitr_in("report_y_b_1.Rmd"), file_out("re…
#> 7 report_y_a… rmarkdown::render(knitr_in("report_y_a_2.Rmd"), file_out("re…
#> 8 report_y_b… rmarkdown::render(knitr_in("report_y_b_2.Rmd"), file_out("re…

# Just for the sake of the reprex:
files <- paste0(plan$target[5:8], ".Rmd")
tmp <- file.create(files)

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-05-13 by the reprex package (v0.2.1)

Alternatively, you can use deparse(substitute()).

library(drake)
plan <- drake_plan(
  y = target(
    f(x),
    transform = cross(f = c(a, b), x = c(1, 2))
  ),
  report = target(
    rmarkdown::render(
      knitr_in(!!paste0(deparse(substitute(y)), ".Rmd")),
      file_out(!!paste0(deparse(substitute(y)), ".html"))
    ),
    transform = map(y)
  )
)  

print(plan)
#> # A tibble: 8 x 2
#>   target       command                                                     
#>   <chr>        <expr>                                                      
#> 1 y_a_1        a(1)                                                       …
#> 2 y_b_1        b(1)                                                       …
#> 3 y_a_2        a(2)                                                       …
#> 4 y_b_2        b(2)                                                       …
#> 5 report_y_a_1 rmarkdown::render(knitr_in("y_a_1.Rmd"), file_out("y_a_1.ht…
#> 6 report_y_b_1 rmarkdown::render(knitr_in("y_b_1.Rmd"), file_out("y_b_1.ht…
#> 7 report_y_a_2 rmarkdown::render(knitr_in("y_a_2.Rmd"), file_out("y_a_2.ht…
#> 8 report_y_b_2 rmarkdown::render(knitr_in("y_b_2.Rmd"), file_out("y_b_2.ht…

# Just for the sake of the reprex:
files <- paste0(plan$target[1:4], ".Rmd")
tmp <- file.create(files)

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-05-13 by the reprex package (v0.2.1)

And as of a9bbcc3917d9773590afbff5611f79d2b7118fa7, you can use .id_chr to get a report for each group of targets.

library(drake)
plan <- drake_plan(
  y = target(
    f(x),
    transform = cross(f = c(a, b), x = c(1, 2))
  ),
  report = target(
    rmarkdown::render(
      knitr_in(!!paste0(.id_chr, ".Rmd")),
      file_out(!!paste0(.id_chr, ".html"))
    ),
    transform = combine(y, .by = f)
  )
)

plan
#> # A tibble: 6 x 2
#>   target   command                                                         
#>   <chr>    <expr>                                                          
#> 1 y_a_1    a(1)                                                           …
#> 2 y_b_1    b(1)                                                           …
#> 3 y_a_2    a(2)                                                           …
#> 4 y_b_2    b(2)                                                           …
#> 5 report_a rmarkdown::render(knitr_in("report_a.Rmd"), file_out("report_a.…
#> 6 report_b rmarkdown::render(knitr_in("report_b.Rmd"), file_out("report_b.…

tmp <- file.create(c("report_a.Rmd", "report_b.Rmd"))

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-05-13 by the reprex package (v0.2.1)

Thanks for the fast response and fix!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wlandau picture wlandau  Â·  8Comments

wlandau-lilly picture wlandau-lilly  Â·  7Comments

hlynurhallgrims picture hlynurhallgrims  Â·  4Comments

wlandau picture wlandau  Â·  8Comments

billdenney picture billdenney  Â·  9Comments