-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
title: "Target Markdown" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include = FALSE} | ||
knitr::opts_chunk$set(collapse = TRUE, comment = "#>") | ||
``` | ||
|
||
Target Markdown is a powerful R Markdown interface for reproducible analysis pipelines, and the chapter at https://books.ropensci.org/targets/markdown.html walks through it in detail. This R Markdown report the example from the chapter. Try it out in both interactive and non-interactive modes, either by running the code chunks in different ways or setting the `tar_interactive` chunk option. | ||
|
||
# Setup | ||
|
||
If you are using old versions of `targets` (<= 0.7.0) and/or `knitr` (<= 1.33), you will need to load the `targets` package in the R Markdown document in order for Target Markdown code chunks to work. | ||
|
||
```{r} | ||
library(targets) | ||
``` | ||
|
||
Near the top of the document, you may also wish to remove the `_targets_r` directory previously written by non-interactive runs of the report. Otherwise, your pipeline may contain superfluous targets. | ||
|
||
```{r} | ||
library(targets) | ||
tar_unscript() | ||
``` | ||
|
||
# Targets | ||
|
||
Our first target borrows the `airquality` dataset built into base R. | ||
|
||
```{targets raw-data} | ||
tar_target(raw_data, airquality) | ||
``` | ||
|
||
Our next targets preprocess the data, make a histogram, and fit a model. | ||
|
||
```{targets downstream-targets} | ||
list( | ||
tar_target(data, {raw_data[complete.cases(airquality), ]}), | ||
tar_target(hist, hist(data$Ozone)) | ||
) | ||
``` | ||
|
||
Set the `tar_simple` chunk option to `TRUE` to define a single target with the command in the code chunk. The chunk below only contains `biglm(Ozone ~ Wind + Temp, data)` in the source, but because `tar_simple` is `TRUE`, it is shorthand for `tar_target(name = fit, command = biglm(Ozone ~ Wind + Temp, data))`. All other arguments to `tar_target()` are set to their default values (configurable with `tar_option_set()`). | ||
|
||
```{targets fit, tar_simple = TRUE} | ||
lm(Ozone ~ Wind + Temp, data) | ||
``` | ||
|
||
# Pipeline | ||
|
||
If you ran all the `{targets}` chunks in non-interactive mode, then your R scripts are set up to run the pipeline. | ||
|
||
```{r} | ||
tar_make() | ||
``` | ||
|
||
# Output | ||
|
||
You can retrieve results from the `_targets/` data store using `tar_read()` or `tar_load()`. | ||
|
||
```{r, message = FALSE} | ||
tar_read(fit) | ||
``` | ||
|
||
```{r} | ||
tar_read(hist) | ||
``` | ||
|
||
|
||
At this point, you can go back and run `{targets}` chunks in interactive mode without interfering with the code or data of the non-interactive pipeline. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters