Target Markdown: non-defaults, relaying, and edge cases

knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

This report is like test-target_markdown_default.Rmd except it uses non-default scripts and data. Try some code chunks interactively, then render this report. Render again to see if the targets correctly skip. Make sure _targets.R, _targets_r/, and _targets/ do not exist. Look for the scripts and data are all in the example/ folder. Delete the HTML file and example/ when you are done.

Packages

The example requires several R packages, and targets must be version 0.5.0.9000 or above.

install.packages(c("biglm", "dplyr", "ggplot2", "readr", "targets", "tidyr"))

Setup

First, load targets to activate the specialized knitr engine for Target Markdown.

library(targets)
knitr::opts_chunk$set(tar_script = "example/script.R")

Near the top, you may also wish to remove the _targets_r directory previously written by non-interactive runs of the report.

tar_unscript()

Globals

We first define some global options/functions common to all targets. The function below plots a histogram of ozone concentrations, and our histogram target will need it.

```{targets example-globals, tar_globals = TRUE} options(tidyverse.quiet = TRUE) tar_option_set( packages = c("biglm", "dplyr", "ggplot2", "readr", "tidyr"), error = "continue" ) create_plot <- function(data) { ggplot(data) + geom_histogram(aes(x = Ozone), bins = 12) + theme_gray(24) }

# Targets

Our first target borrows the `airquality` dataset built into base R.

```{targets othername, tar_name = "raw_data", tar_simple = TRUE}
airquality

Warnings, messages, and errors are relayed in both modes.

```{targets raw_data_interactive, tar_simple = TRUE, error = TRUE, message = TRUE, output = TRUE, tar_interactive = TRUE} print("output_interactive") message("message_interactive") warning("warning_interactive") stop("error_interactive") airquality

```{targets raw_data_noninteractive, tar_simple = TRUE, error = TRUE, message = TRUE, output = TRUE, tar_interactive = FALSE}
print("output_noninteractive")
message("message_noninteractive")
warning("warning_noninteractive")
stop("error_noninteractive")
airquality

Our next targets preprocess the data, make a histogram, and fit a model.

```{targets downstream-targets} list( tar_target(data, raw_data %>% filter(!is.na(Ozone))), tar_target(hist, create_plot(data)), tar_target(fit, biglm(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(script = "example/script.R", store = "example/store")

Output

You can retrieve results from the _targets/ data store using tar_read() or tar_load().

library(biglm)
tar_read(fit, store = "example/store")
tar_read(hist, store = "example/store")

The targets dependency graph helps your readers understand the steps of your pipeline at a high level.

tar_visnetwork(script = "example/script.R", store = "example/store")

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.



Try the targets package in your browser

Any scripts or data that you put into this service are public.

targets documentation built on Oct. 12, 2023, 5:07 p.m.