Skip to content

Commit

Permalink
Finish targets lecture note (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
boyiguo1 committed Dec 1, 2022
1 parent fe09693 commit 986a887
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "71977170a32607ab4bbc95716c0fcd20",
"hash": "9347f7061cc9c85f6421989c1c04c14b",
"result": {
"markdown": "---\ntitle: \"Reproducibile Workflows with `targets`\"\nauthor: \n - name: Boyi Guo\n url: https://boyiguo1.github.io\n affiliation: Department of Biostatistics, Johns Hopkins\n affiliation_url: https://publichealth.jhu.edu\ndescription: \"A Make-line pipeline tool for creating reproducible workflows in R\"\ndate: 2022-12-01\ndraft: true\ncategories: [module 3, week 6, project management, targets]\nexecute:\n eval: false\n---\n\n\n\n\n<!-- Add interesting quote -->\n\n# Pre-lecture materials\n\n### Read ahead\n\n::: callout-note\n## Read ahead\n\n**Before class, you can prepare by reading the following materials:**\n\n1. <https://books.ropensci.org/targets/>\n2. <https://books.ropensci.org/targets/walkthrough.html>\n3. <https://boyiguo1.github.io/talks/2020-07-16-Reproducible-Analysis-Workflow.pdf>\n:::\n\n### Prerequisites\nBefore starting you must install the additional package:\n\n* `targets` - the R Workflows package\n* `usethis` - an automation package that simplifies project creation and setup\n* `renv` - a package manager in R\n\nYou can do this by calling\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"usethis\")\ninstall.packages(\"targets\")\ninstall.packages(\"renv\")\n```\n:::\n\n\nor use the “Install Packages…” option from the “Tools” menu in RStudio.\n\n### Acknowledgements\n\nMaterial for this lecture was borrowed and adopted from\n\n- https://books.ropensci.org/targets/walkthrough.html\n\n# Learning objectives\n\n::: callout-note\n# Learning objectives\n\n**At the end of this lesson you will:**\n\n- setup up `targets` analytic pipeline\n- write and run a data analysis with `targets`\n- replicate and retrieve analysis results from a `targets` workflow\n:::\n\n# What is `targets`?\n\n\n# Why do we use `targets`?\n\n\n# How to use `targets`\n\n## Set up a `targets` workflow\n\n::: {.cell}\n\n```{.r .cell-code}\n# Start a new R project\nusethis::create_project(\"targets_eg\")\n# Config target workflow\ntargets::use_targets()\n```\n:::\n\n\n### (Optional) Version control packages with `renv`\n\n::: {.cell}\n\n```{.r .cell-code}\n# Config renv system\nrenv::init()\ntargets::tar_renv()\n```\n:::\n\n\nIf other people opens up this project on a different computer, `renv` will automatically install all the necessary packages, i.e. the same version.\n\n::: callout-tip\n### Important `renv` functions\nYou need to keep track of your R packages in this analysis, similar to you version control your files using `git`. You may need to call the following functions periodically, i.e. after you add/remove necessary packages.\n* `targets::tar_renv()` updates `_targets_packages.R` by gathering all packages in your analytic workflow \n* `renv::status()` shows which packages are outdated or not recorded\n* `renv::snapshot()` updates your packages version number\n* `renv::restore()` restores all missing packages or packages whose version number doesn't match with the most updated snapshot.\n\nFor more information, visit <https://rstudio.github.io/renv/articles/renv.html>\n:::\n\n## Understand the file system\n\n\n\n\n\n# Post-lecture materials\n\n### Final Questions\n\nHere are some post-lecture questions to help you think about the material discussed.\n\n::: callout-note\n### Questions\n\n1. Add here.\n:::\n\n### Additional Resources\n\n::: callout-tip\n- `targets` website <https://docs.ropensci.org/targets/>\n- The `targetopic`, a `targets` ecosystem <https://wlandau.github.io/targetopia/packages.html>\n- A tutorial & reproducible example on calculating residential segregation indices with decennial US census data <https://github.com/boyiguo1/Tutorial-Residential_Segregation_Score>\n:::\n",
"markdown": "---\ntitle: \"Reproducibile Workflows with `targets`\"\nauthor: \n - name: Boyi Guo\n url: https://boyiguo1.github.io\n affiliation: Department of Biostatistics, Johns Hopkins\n affiliation_url: https://publichealth.jhu.edu\ndescription: \"A Make-line pipeline tool for creating reproducible workflows in R\"\ndate: 2022-12-01\ncategories: [module 3, week 6, project management, targets]\nexecute:\n eval: false\n---\n\n\n\n\n<!-- Add interesting quote -->\n\n# Pre-lecture materials\n\n### Read ahead\n\n::: callout-note\n## Read ahead\n\n**Before class, you can prepare by reading the following materials:**\n\n1. <https://books.ropensci.org/targets/>\n2. <https://books.ropensci.org/targets/walkthrough.html>\n3. <https://boyiguo1.github.io/talks/2020-07-16-Reproducible-Analysis-Workflow.pdf>\n:::\n\n### Prerequisites\nBefore starting you must install the additional package:\n\n* `targets` - the R Workflows package\n* `usethis` - an automation package that simplifies project creation and setup\n* `renv` - a package manager in R\n\nYou can do this by calling\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"usethis\")\ninstall.packages(\"targets\")\ninstall.packages(\"renv\")\n```\n:::\n\n\nor use the “Install Packages…” option from the “Tools” menu in RStudio.\n\n### Acknowledgements\n\nMaterial for this lecture was borrowed and adopted from\n\n- https://books.ropensci.org/targets/walkthrough.html\n\n# Learning objectives\n\n::: callout-note\n# Learning objectives\n\n**At the end of this lesson you will:**\n\n- setup up `targets` analytic pipeline\n- write and run a data analysis with `targets`\n- replicate and retrieve analysis results from a `targets` workflow\n:::\n\n# What is `targets`?\n<div style=\"width:100%;height:0;padding-bottom:100%;position:relative;\"><iframe src=\"https://giphy.com/embed/l0He7QpY9OzTyEAjm\" width=\"100%\" height=\"100%\" style=\"position:absolute\" frameBorder=\"0\" class=\"giphy-embed\" allowFullScreen></iframe></div>\n\n`targets` is not a grocery store. Instead, it is a R package that will you to manage your analysis. You can think of `targets` as a butler that help you to manage your analytics. These services include\n\n* to manage the order of your analysis workflow so you won't mess up the analytic steps\n* to save the output of each analytic procedures so you don't have to wait for the repetitive runs\n* to monitor the change in your code so you can only update the analysis a change happened\n* to reproduce the who analysis with a click so you don't have to run multiple scirpts\n\n# Why do we use `targets`?\n<iframe src='https://gfycat.com/ifr/NeedyRipeKingbird' frameborder='0' scrolling='no' allowfullscreen width='640' height='404'></iframe>\n\n`targets` helps us to be more efficient at managing an analytic workflow and improving productivity without driving ourselves crazy. Let me simply put this way, managing file names for your code or saved objects can be very painful. But `targets` can help you handle that. \n\n<div style=\"width:100%;height:0;padding-bottom:100%;position:relative;\"><iframe src=\"https://giphy.com/embed/jtFOBTQhSUmR5STTZx\" width=\"100%\" height=\"100%\" style=\"position:absolute\" frameBorder=\"0\" class=\"giphy-embed\" allowFullScreen></iframe></div>\n\n# How to use `targets`\n\n[The {targets} R package user manual](https://books.ropensci.org/targets/) is a great source to learn how to use `targets`. The intro level of `targets` tutorial is well documented in [*Chapter 2 Walkthrough*](https://books.ropensci.org/targets/walkthrough.html). Instead going through the chapter with you, I will focus on some tricks that it is not discussed in the user manual. \n\n## Set up a `targets` workflow\n\n::: {.cell}\n\n```{.r .cell-code}\n# Start a new R project\nusethis::create_project(\"targets_eg\")\n# Config target workflow\ntargets::use_targets()\n```\n:::\n\n\n### (Optional) Version control packages with `renv`\n\n::: {.cell}\n\n```{.r .cell-code}\n# Config renv system\nrenv::init()\ntargets::tar_renv()\n```\n:::\n\n\nIf other people opens up this project on a different computer, `renv` will automatically install all the necessary packages, i.e. the same version.\n\n::: callout-tip\n### Important `renv` functions\nYou need to keep track of your R packages in this analysis, similar to you version control your files using `git`. You may need to call the following functions periodically, i.e. after you add/remove necessary packages.\n* `targets::tar_renv()` updates `_targets_packages.R` by gathering all packages in your analytic workflow \n* `renv::status()` shows which packages are outdated or not recorded\n* `renv::snapshot()` updates your packages version number\n* `renv::restore()` restores all missing packages or packages whose version number doesn't match with the most updated snapshot.\n\nFor more information, visit <https://rstudio.github.io/renv/articles/renv.html>\n:::\n\n## Set up keyboard shortcuts\n`targets` programmed some `addins` to help user navigate through workflow management with graphic user interface. For example, if you click on the `Addins` button in the tool bar which locates on the top of the RStudio window, you can see many options that help you to work with `targets`\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![A screenshot of addins for `targets`](../../images/targets/Addins.png){fig-align='center' width=316}\n:::\n:::\n\n\nWith these addins, you don't necessarily remember all the functions to run `targets`, such as `targets::tar_make()`, `targets::tar_load()`, `targets::tar_visnetwork()`, etc.\n\nIf you are comfortable with keyboard shortcuts, you can set up for these commoonly used functions. In order to do that, you need to go to `Tools` -> `Modify Keyboard Shortcuts`.\n\n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![A screenshot of how to modify keyboard shortcuts](../../images/targets/ModifyShortcuts.png){fig-align='center' width=382}\n:::\n:::\n\n\nWith in the pop-up keyboard shortcuts menu, you can search `addin` or `targets` or a specific `target` addin function, e.g. `Load target at cursor` in the highlighted search box. You can customize the keyboard shortcut by clicking on the input box within the `Shortcut` column. \n\n\n::: {.cell layout-align=\"center\"}\n::: {.cell-output-display}\n![A screenshot of keyboard shortcuts menu](../../images/targets/ShortcutsMenu.png){fig-align='center' width=801}\n:::\n:::\n\n\n\n# Summary\n`targets` is a workflow management powerhouse. It offers much more utility than we covered today. [The {targets} R package user manual](https://books.ropensci.org/targets/) does an excellent job on explaining how to set up parallel computing with the system, to work with markdown systems (I managed my dissertation writing in `targets`), and many more. \n\nNevertheless, I need to warn you that learning `targets` seems intimidating at the beginning because of the setup process and new syntax. It may take multiple projects or iterations until you are comfortable using it. \n\n<div style=\"width:100%;height:0;padding-bottom:63%;position:relative;\"><iframe src=\"https://giphy.com/embed/gn0iBkQjUG092\" width=\"100%\" height=\"100%\" style=\"position:absolute\" frameBorder=\"0\" class=\"giphy-embed\" allowFullScreen></iframe></div>\n\nBut it is very rewarding and can save you a lot of time in the long run!\n\n\n### Additional Resources\n\n::: callout-tip\n- `targets` website <https://docs.ropensci.org/targets/>\n- The `targetopic`, a `targets` ecosystem <https://wlandau.github.io/targetopia/packages.html>\n- A tutorial & reproducible example on calculating residential segregation indices with decennial US census data <https://github.com/boyiguo1/Tutorial-Residential_Segregation_Score>\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
Binary file added images/targets/Addins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/targets/ModifyShortcuts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/targets/ShortcutsMenu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 48 additions & 9 deletions posts/2022-12-01-targets-proj-workflows/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ author:
affiliation_url: https://publichealth.jhu.edu
description: "A Make-line pipeline tool for creating reproducible workflows in R"
date: 2022-12-01
draft: true
categories: [module 3, week 6, project management, targets]
execute:
eval: false
Expand Down Expand Up @@ -72,13 +71,26 @@ Material for this lecture was borrowed and adopted from
:::

# What is `targets`?
<div style="width:100%;height:0;padding-bottom:100%;position:relative;"><iframe src="https://giphy.com/embed/l0He7QpY9OzTyEAjm" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div>

`targets` is not a grocery store. Instead, it is a R package that will you to manage your analysis. You can think of `targets` as a butler that help you to manage your analytics. These services include

* to manage the order of your analysis workflow so you won't mess up the analytic steps
* to save the output of each analytic procedures so you don't have to wait for the repetitive runs
* to monitor the change in your code so you can only update the analysis a change happened
* to reproduce the who analysis with a click so you don't have to run multiple scirpts

# Why do we use `targets`?
<iframe src='https://gfycat.com/ifr/NeedyRipeKingbird' frameborder='0' scrolling='no' allowfullscreen width='640' height='404'></iframe>

`targets` helps us to be more efficient at managing an analytic workflow and improving productivity without driving ourselves crazy. Let me simply put this way, managing file names for your code or saved objects can be very painful. But `targets` can help you handle that.

<div style="width:100%;height:0;padding-bottom:100%;position:relative;"><iframe src="https://giphy.com/embed/jtFOBTQhSUmR5STTZx" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div>

# How to use `targets`

[The {targets} R package user manual](https://books.ropensci.org/targets/) is a great source to learn how to use `targets`. The intro level of `targets` tutorial is well documented in [*Chapter 2 Walkthrough*](https://books.ropensci.org/targets/walkthrough.html). Instead going through the chapter with you, I will focus on some tricks that it is not discussed in the user manual.

## Set up a `targets` workflow
```{r}
# Start a new R project
Expand Down Expand Up @@ -107,23 +119,50 @@ You need to keep track of your R packages in this analysis, similar to you versi
For more information, visit <https://rstudio.github.io/renv/articles/renv.html>
:::

## Understand the file system
## Set up keyboard shortcuts
`targets` programmed some `addins` to help user navigate through workflow management with graphic user interface. For example, if you click on the `Addins` button in the tool bar which locates on the top of the RStudio window, you can see many options that help you to work with `targets`

```{r}
#| eval: true
#| echo: false
#| fig-cap: 'A screenshot of addins for `targets`'
#| fig-align: 'center'
knitr::include_graphics("../../images/targets/Addins.png")
```

With these addins, you don't necessarily remember all the functions to run `targets`, such as `targets::tar_make()`, `targets::tar_load()`, `targets::tar_visnetwork()`, etc.

If you are comfortable with keyboard shortcuts, you can set up for these commoonly used functions. In order to do that, you need to go to `Tools` -> `Modify Keyboard Shortcuts`.


# Post-lecture materials
```{r}
#| eval: true
#| echo: false
#| fig-cap: 'A screenshot of how to modify keyboard shortcuts'
#| fig-align: 'center'
knitr::include_graphics("../../images/targets/ModifyShortcuts.png")
```

### Final Questions
With in the pop-up keyboard shortcuts menu, you can search `addin` or `targets` or a specific `target` addin function, e.g. `Load target at cursor` in the highlighted search box. You can customize the keyboard shortcut by clicking on the input box within the `Shortcut` column.

Here are some post-lecture questions to help you think about the material discussed.
```{r}
#| eval: true
#| echo: false
#| fig-cap: 'A screenshot of keyboard shortcuts menu'
#| fig-align: 'center'
knitr::include_graphics("../../images/targets/ShortcutsMenu.png")
```

::: callout-note
### Questions

1. Add here.
:::
# Summary
`targets` is a workflow management powerhouse. It offers much more utility than we covered today. [The {targets} R package user manual](https://books.ropensci.org/targets/) does an excellent job on explaining how to set up parallel computing with the system, to work with markdown systems (I managed my dissertation writing in `targets`), and many more.

Nevertheless, I need to warn you that learning `targets` seems intimidating at the beginning because of the setup process and new syntax. It may take multiple projects or iterations until you are comfortable using it.

<div style="width:100%;height:0;padding-bottom:63%;position:relative;"><iframe src="https://giphy.com/embed/gn0iBkQjUG092" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div>

But it is very rewarding and can save you a lot of time in the long run!


### Additional Resources

Expand Down

0 comments on commit 986a887

Please sign in to comment.