carpentries-incubator / targets-workshop

Pre-alpha {targets} workshop
https://carpentries-incubator.github.io/targets-workshop/
Other
33 stars 6 forks source link

Rethinking the `_targets.R` approach #16

Closed multimeric closed 1 year ago

multimeric commented 1 year ago

The currently used approach to displaying and then running targets workflows in the lesson is to:

An example can be seen here: https://github.com/carpentries-incubator/targets-workshop/blob/4588d719f590d134aea783152cd1bd4c7695f012/episodes/branch.Rmd#L69-L101.

It occurred to me that we could maybe improve this by using some knitr features, and avoid the issue of needing to synchronise the two copies of the script.

First we move all the example workflows (_targets.R) files into a directory, let's say episodes/files/plan_3.R. Then in the Rmd we use the file= option to knitr, which loads the chunk from a file on disk and displays it. This does work with sandpaper, I've verified it myself:

```{r, file="episodes/files/plan_3.R"}

Then, when we need to run the workflow, we add an invisible chunk (similar to before), but this time it looks like:
```R
 tar_dir({ 
   tar_make(script="episodes/files/plan_3.R")
 }) 

This simplifies both of the chunks quite a lot, should increase maintainability, and makes it easier to edit the sample workflows. Thoughts?

joelnitta commented 1 year ago

Thanks for this idea!

I knew my original approach was brittle because we have two copies of the targets plan, but didn't have a good solution at the time.

Unfortunately I don't have time to work on this ATM (probably not for a few more weeks), but this sounds good to me. If you want to refactor (or start on refactoring) the code that would be great.

multimeric commented 1 year ago

Great! That's okay, I should have time to write a PR for this in the next week, I just wanted to run it by you first.

joelnitta commented 1 year ago

Awesome, thanks!