3

首先,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目标有保存情节的副作用。有一个更好的方法吗?像一个合适file_outplot目标?

谢谢你。

4

1 回答 1

3

好问题。考虑到这一点实际上可以帮助我解决drake+的一些问题keras

如何file_out()添加

你快到了,你所需要的只是一些整洁的评估 ( !!) 以确保每个文件名都是计划中的文字字符串。

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 包(v0.2.1)于 2019 年 3 月 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 包(v0.2.1)于 2019 年 3 月 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 包(v0.2.1)于 2019 年 3 月 26 日创建

于 2019-03-27T01:39:00.563 回答