class: center, middle, inverse, title-slide # Make-like declarative workflows with
## Exercises ### Kirill Müller ### Zurich, 2018-03-05 --- # The *cooking* package https://krlmlr.github.io/cooking https://krlmlr.github.io/cooking/reference/cooking-package.html ```r buy(what, from = supermarket()) supermarket() peel(what, ...) chop(what, ...) fry(what, ...) cook(what, ...) combine(..., name = "ragout") make_cooking(processed) ``` --- # Recipe snippets ```r library(cooking) chopped_meat <- chop("raw_meat.csv") chopped_meat ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped meat, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> raw meat</div></code> ```r fry(chopped_meat, with = "vegetables") ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> fried meat, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped meat, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> raw meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> vegetables</div></code> ```r peel(buy("carrots")) ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> peeled carrots, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> raw carrots, <span style="font-style:italic">bought at</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> supermarket</div></code> --- # New methods ```r dice <- make_cooking("diced", "cut from") dice("tomatoes") ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> diced tomatoes, <span style="font-style:italic">cut from</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> tomatoes</div></code> --- background-image: url(dots/detailed.png) background-size: contain background-position: 0% 90% # Exercises https://krlmlr.shinyapps.io/cooking-tutorial/ 1. Use `buy()` to buy meat 2. Chop this meat 3. Fry the chopped meat with oil 4. Combine the fried meat with vegetables 5. Add a salad --- # The *drake* package https://ropensci.github.io/drake ```r # Create plan `drake_plan()` # Execute plan `make()` # Read single target `readd()` # From plan to configuration `drake_config()` # Visualize `vis_drake_graph()` # What do we need to run? `outdated()` # What if a run fails? `diagnose()` ``` --- background-image: url(dots/detailed.png) background-size: contain background-position: 0% 90% ```r library(drake) *plan <- drake_plan( meat = buy("meat"), chopped_meat = chop(meat), fried_meat = fry(chopped_meat, with = "oil"), ragout = combine(fried_meat, with = "vegetables") ) plan ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#8a8a8a"># A tibble: 4 x 2</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> target command </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="font-style:italic"><span style="color:#8a8a8a"><chr></span></span> <span style="font-style:italic"><span style="color:#8a8a8a"><chr></span></span> </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#b2b2b2">1</span> meat <span style="color:#8a8a8a">"</span>buy(\"meat\")<span style="color:#8a8a8a">"</span> </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#b2b2b2">2</span> chopped_meat chop(meat) </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#b2b2b2">3</span> fried_meat <span style="color:#8a8a8a">"</span>fry(chopped_meat, with = \"oil\")<span style="color:#8a8a8a">"</span> </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#b2b2b2">4</span> ragout <span style="color:#8a8a8a">"</span>combine(fried_meat, with = \"vegetables\")<span style="color:#8a8a8a">"</span></div></code> --- background-image: url(dots/detailed.png) background-size: contain background-position: 0% 90% ```r *make(plan) ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">cache</span> .../.drake</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> Unloading targets from environment:</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">connect</span> 4 imports: pre_chunk, dice, colourise_chunk, plan</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">connect</span> 4 targets: meat, chopped_meat, fried_meat, ragout</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">check</span> 4 items: buy, chop, combine, fry</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">check</span> 1 item: meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#00afff">target</span> meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">check</span> 1 item: chopped_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#00afff">target</span> chopped_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">check</span> 1 item: fried_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#ff5fff">unload</span> 1 item: meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#00afff">target</span> fried_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">check</span> 1 item: ragout</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#ff5fff">unload</span> 1 item: chopped_meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#00afff">target</span> ragout</div></code> --- background-image: url(dots/detailed.png) background-size: contain background-position: 0% 90% ```r *readd(ragout) ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> <span style="color:#afd7d7">cache</span> .../.drake</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> ragout, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> fried meat, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped meat, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> raw meat, <span style="font-style:italic">bought at</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> supermarket</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> oil</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> vegetables</div></code> --- ```r *config <- drake_config(plan, verbose = FALSE) *vis_drake_graph(config) ```
--- ```r config <- drake_config(plan, verbose = FALSE) *outdated(config) ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> character(0)</div></code> --- background-image: url(dots/full.png) background-size: contain background-position: 0% 90% # Exercises https://krlmlr.shinyapps.io/cooking-drake-tutorial/ 1. Plan to buy meat with `buy()` 2. Plan to chop and fry the meat 3. Fry with garlic instead 4. Buy at the butcher's instead 5. Prepare vegetables 6. Combine the ragout 7. How to **not** combine the ragout --- # *drake* and files ```r library(drake) plan <- drake_plan( * meat = buy(readLines(file_in("raw-meat.txt"))), chopped_meat = chop(meat), fried_meat = fry(chopped_meat, with = "oil"), ragout = combine(fried_meat, with = "vegetables"), * saveRDS(ragout, file_out("ragout.rds")), rmarkdown::render( * knitr_in("ragout.Rmd"), output_file = file_out("ragout.md") ) ) ``` --- # Exercises https://github.com/krlmlr/drake-sib-zurich/tree/master/cooking-with-files 1. Save `make.R` as `make-solution.R`, do not edit `make.R`. 2. Use files to describe the meat and store the ragout. 3. Run the script. 4. Edit the `raw-meat.txt` file. 5. Run `make()`. Do the results align with your expectations? 6. Create a new text file and use it instead of `raw-meat.txt`. Run `make()` again. 7. Open the file `cooking-with-files.Rmd`, knit it. How does the code in the `.Rmd` document access the targets we just built? 8. Extend the plan to knit the document via `rmarkdown::render()`. Remember to use `knitr_in()` for the input file. 9. Verify that the `.Rmd` is processed if you change inputs or commands. --- --- # The pipe ```r "carrots" %>% buy() %>% peel() %>% chop() %>% fry(with = c("oil", "garlic", "chopped meat")) %>% combine("mashed potatoes") ``` <code class="remark-code r"><div class="remark-code-line"><span style="color:#8a8a8a">#></span> ragout, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> fried carrots, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped carrots, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> peeled carrots, <span style="font-style:italic">made of</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> raw carrots, <span style="font-style:italic">bought at</span></div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> supermarket</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> oil </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> garlic </div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> chopped meat</div> <div class="remark-code-line"><span style="color:#8a8a8a">#></span> mashed potatoes</div></code>