drake - 映射到 ggplot 目标以输出它们

drake - map over ggplot targets to output them

首先,drake 太神奇了。我喜欢设计依赖图然后一举执行的工作流程。

可是,我运行进了一个路障。

我的工作流程是在大参数网格上进行模拟,然后汇总所述网格的不同切片。我想为每个这样的切片创建一个图。如果我理解正确,我应该使用某种形式的 cross->combine->map 来实现这一点。

这是我的:

sim_data <- function(mean, sd) {
  tibble(r = rnorm(1000, mean, sd))
}

plot_dis <- function(lg, title) {
  ggplot(lg) + 
    geom_histogram(aes(x=r, fill=sd), binwidth = 0.25) + 
    labs(title = str_glue("x = {title}")) +
    ggsave(str_glue("{title}.pdf")) # side-effect
}

plan <- drake_plan(
  data = target(
    sim_data(mean = x, sd = sd),
    transform = cross(x = c(10, 20, 30), sd = c(1, 2))
  ), # awesome
  s_x = target(
    bind_rows(data, .id = "sd"),
    transform = combine(data, .by=x)
  ), # great
  plot = target(
    plot_dis(s_x, x),
    transform = map(s_x)
  ) # how to add a `file_out` to this target?
)

所以我的 plot 目标有保存情节的副作用。 有一个更好的方法吗?像 plot 目标的正确 file_out 吗?

谢谢。

好问题。考虑这个实际上帮助我解决了 drake + keras.

的一些问题

如何添加file_out()s

您就快完成了,您只需要进行一些整洁的评估 (!!) 以确保每个文件名都是计划中的文字字符串。

library(drake)
drake_plan(
  data = target(
    sim_data(mean = x, sd = sd),
    transform = cross(x = c(10, 20, 30), sd = c(1, 2))
  ),
  s_x = target(
    bind_rows(data, .id = "sd"),
    transform = combine(data, .by=x)
  ),
  plot = target(
    plot_dis(s_x, file_out(!!sprintf("%s.pdf", x))),
    transform = map(s_x)
  )
)
#> # A tibble: 12 x 2
#>    target      command                                    
#>    <chr>       <expr>                                     
#>  1 data_10_1   sim_data(mean = 10, sd = 1)                
#>  2 data_20_1   sim_data(mean = 20, sd = 1)                
#>  3 data_30_1   sim_data(mean = 30, sd = 1)                
#>  4 data_10_2   sim_data(mean = 10, sd = 2)                
#>  5 data_20_2   sim_data(mean = 20, sd = 2)                
#>  6 data_30_2   sim_data(mean = 30, sd = 2)                
#>  7 s_x_10      bind_rows(data_10_1, data_10_2, .id = "sd")
#>  8 s_x_20      bind_rows(data_20_1, data_20_2, .id = "sd")
#>  9 s_x_30      bind_rows(data_30_1, data_30_2, .id = "sd")
#> 10 plot_s_x_10 plot_dis(s_x_10, file_out("10.pdf"))       
#> 11 plot_s_x_20 plot_dis(s_x_20, file_out("20.pdf"))       
#> 12 plot_s_x_30 plot_dis(s_x_30, file_out("30.pdf"))

reprex package (v0.2.1)

于 2019-03-26 创建

通过多一点元编程,您可以使用整个目标名称。

library(drake)
drake_plan(
  data = target(
    sim_data(mean = x, sd = sd),
    transform = cross(x = c(10, 20, 30), sd = c(1, 2))
  ),
  s_x = target(
    bind_rows(data, .id = "sd"),
    transform = combine(data, .by=x)
  ),
  plot = target(
    plot_dis(s_x, file_out(!!sprintf("%s.pdf", deparse(substitute(s_x))))),
    transform = map(s_x)
  )
)
#> # A tibble: 12 x 2
#>    target      command                                    
#>    <chr>       <expr>                                     
#>  1 data_10_1   sim_data(mean = 10, sd = 1)                
#>  2 data_20_1   sim_data(mean = 20, sd = 1)                
#>  3 data_30_1   sim_data(mean = 30, sd = 1)                
#>  4 data_10_2   sim_data(mean = 10, sd = 2)                
#>  5 data_20_2   sim_data(mean = 20, sd = 2)                
#>  6 data_30_2   sim_data(mean = 30, sd = 2)                
#>  7 s_x_10      bind_rows(data_10_1, data_10_2, .id = "sd")
#>  8 s_x_20      bind_rows(data_20_1, data_20_2, .id = "sd")
#>  9 s_x_30      bind_rows(data_30_1, data_30_2, .id = "sd")
#> 10 plot_s_x_10 plot_dis(s_x_10, file_out("s_x_10.pdf"))   
#> 11 plot_s_x_20 plot_dis(s_x_20, file_out("s_x_20.pdf"))   
#> 12 plot_s_x_30 plot_dis(s_x_30, file_out("s_x_30.pdf"))

reprex package (v0.2.1)

于 2019-03-26 创建

但是你真的需要文件吗?

ggplot2 对象与 drake 的缓存配合得很好。

library(drake)
library(tidyverse)

sim_data <- function(mean, sd) {
  tibble(r = rnorm(1000, mean, sd))
}

plot_dis <- function(lg) {
  ggplot(lg) + 
    geom_histogram(aes(x=r, fill=sd), binwidth = 0.25) + 
    labs(title = deparse(substitute(lg)))
}

plan <- drake_plan(
  data = target(
    sim_data(mean = x, sd = sd),
    transform = cross(x = c(10, 20, 30), sd = c(1, 2))
  ),
  s_x = target(
    bind_rows(data, .id = "sd"),
    transform = combine(data, .by=x)
  ),
  plot = target(
    plot_dis(s_x),
    transform = map(s_x)
  )
)

make(plan)
#> target data_10_1
#> target data_10_2
#> target data_20_1
#> target data_20_2
#> target data_30_2
#> target data_30_1
#> target s_x_10
#> target s_x_20
#> target s_x_30
#> target plot_s_x_10
#> target plot_s_x_20
#> target plot_s_x_30

readd(plot_s_x_10) # see also loadd()

reprex package (v0.2.1)

于 2019-03-26 创建