install.packages( c("usethis", "targets", "renv"))Pre-lecture materials
Read ahead
Before class, you can prepare by reading the following materials:
Prerequisites
Before starting you must install the additional package:
targets- the R Workflows packageusethis- an automation package that simplifies project creation and setuprenv- a package manager in R
You can do this by calling
or use the “Install Packages…” option from the “Tools” menu in RStudio.
Acknowledgements
Material for this lecture was borrowed and adopted from
- https://books.ropensci.org/targets/walkthrough.html
Special Message from Boyi
To students attending online:
I apologize for the scraping sound I made during last lecture.
Learning objectives
At the end of this lesson you will:
- set up
targetsanalytic pipeline - write and run a data analysis with
targets - replicate and retrieve analysis results from a
targetsworkflow
What is targets?
targets is not the grocery store. Instead, it is a R package that helps you to manage your analysis. You can think of targets as a butler who helps you to manage your analytics. These services include
- to manage the ordering of your analysis so you won’t confuse about the steps when reproducing an analysis
- to save the output of each analytic procedures so you don’t have to wait for repetitive runs of static results
- to monitor the change in your code so you can update the only analytic tasks that changes are made
- to reproduce the whole analysis with a click so you don’t have to run multiple scripts
Why do we use targets?
targets helps us be more efficient at managing analytic workflows, and hence improve productivity with bare minimum efforts. 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 when use in combination with other version control system git.
How to use targets
The {targets} R package user manual is a great source to learn how to use targets. The intro level of targets tutorial is well documented in Chapter 2 Walkthrough. Instead going through the chapter with you, I will focus on some tricks that is not discussed in the user manual.
Set up a targets workflow
# Start a new R project
usethis::create_project("targets_eg")
# Config target workflow
targets::use_targets()(Optional) Version control packages with renv
# Config renv system
renv::init()
targets::tar_renv()If other people opens up this project on a different computer, renv will automatically install all the necessary packages, especially the same versions of those packages.
renv functions
Idealistically, you need to keep track of your R packages in every 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.
targets::tar_renv()updates_targets_packages.Rby gathering all packages in your analytic workflowrenv::status()shows which packages are outdated or not recordedrenv::snapshot()updates your packages version number by taking a snapshot of your project libraryrenv::restore()restores all missing packages or packages whose version number doesn’t match with the most updated snapshot.
For more information, visit https://rstudio.github.io/renv/articles/renv.html
Set up keyboard shortcuts
targets provide some addins to help users navigate through workflow management with a click-and-point system. For example, if you click on the Addins button in the tool bar (highlighted in the screen capture below) which locates on the top of the RStudio window, you can see many options that help you to work with targets

targetsWith these addins, you don’t necessarily have to remember all the functions to run targets, such as targets::tar_make(), targets::tar_load(), targets::tar_visnetwork(), etc.
If you prefer keyboard shortcuts, you can set up for these commonly used functions. In order to do that, you need to go to Tools -> Modify Keyboard Shortcuts.

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 search box. You can customize the keyboard shortcut by clicking on the input box within the Shortcut column.

Summary
targets is a workflow management powerhouse. It offers much more utility than we covered today. The {targets} R package user manual 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 could be intimidating at the beginning because of the setup process and new syntax. It may take multiple iterations or projects until you are comfortable using it.
But it is very rewarding and can save you a lot of time in the long run! It is a worthy investment of time.
Additional Resources
targetswebsite https://docs.ropensci.org/targets/- The
targetopic, atargetsecosystem https://wlandau.github.io/targetopia/packages.html - A tutorial & reproducible example on calculating residential segregation indices with decennial US census data https://github.com/boyiguo1/Tutorial-Residential_Segregation_Score