install.packages( c("usethis", "targets", "renv"))
Pre-lecture materials
Read ahead
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
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
::create_project("targets_eg")
usethis# Config target workflow
::use_targets() targets
(Optional) Version control packages with renv
# Config renv system
::init()
renv::tar_renv() targets
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.
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
With 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.