Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmarkdown::render with transform #866

Closed
htlin opened this issue May 13, 2019 · 3 comments
Closed

rmarkdown::render with transform #866

htlin opened this issue May 13, 2019 · 3 comments

Comments

@htlin
Copy link

htlin commented May 13, 2019

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.

@wlandau
Copy link
Member

wlandau commented May 13, 2019

.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)

@wlandau
Copy link
Member

wlandau commented May 13, 2019

And as of a9bbcc3, 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)

@wlandau wlandau closed this as completed May 13, 2019
@htlin
Copy link
Author

htlin commented May 13, 2019

Thanks for the fast response and fix!

@nettoyoussef nettoyoussef mentioned this issue Jun 13, 2019
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants