Chapter 5 Functions

targets expects users to adopt a function-oriented style of programming. User-defined R functions are essential to express the complexities of data generation, analysis, and reporting. This chapter explains what makes functions useful and how to leverage them in your pipelines.

5.1 Problems with script-based workflows

Traditional data analysis projects consist of imperative scripts, often with with numeric prefixes.

01-data.R
02-model.R
03-plot.R

To run the project, the user runs each of the scripts in order.

source("01-data.R")
source("02-model.R")
source("03-plot.R")

Each script executes a different part of the workflow.

# 01-data.R
library(tidyverse)
raw_data <- "data/raw_data.csv" %>%
  read_csv(col_types = cols()) %>%
  filter(!is.na(Ozone))
write_csv(data, "data/data.csv")
# 02-model.R
library(biglm)
library(tidyverse)
data <- read_csv("data/data.csv", col_types = cols())
fit <- biglm(Ozone ~ Wind + Temp, data)
saveRDS(fit, "fit.rds")
# 03-plot.R
library(tidyverse)
data <- read_csv("data/data.csv", col_types = cols())
hist <- ggplot(data) +
  geom_histogram(aes(x = Ozone)) +
  theme_gray(24)
ggsave("hist.png", hist)

Although this approach may feel convenient at first, it scales poorly for medium-sized workflows. These imperative scripts are monolithic, and they grow too large and complicated to understand or maintain.

5.2 Functions

Functions are the building blocks of most computer code. They make code easier to think about, and they break down complicated ideas into small manageable pieces. Out of context, you can develop and test a function in isolation without mentally juggling the rest of the project. In the context of the whole workflow, functions are convenient shorthand to make your work easier to read.

In addition, functions are a nice mental model to express data science. A data analysis workflow is a sequence of transformations: datasets map to analyses, and analyses map to summaries. In fact, a function for data science typically falls into one of three categories:

  1. Process a dataset.
  2. Analyze a dataset.
  3. Summarize an analysis.

5.3 Writing functions

Let us begin with our imperative code for data processing. Every time you look at it, you need to read it carefully and relearn what it does. And test it, you need to copy the entire block into the R console.

raw_data <- "data/raw_data.csv" %>%
  read_csv(col_types = cols()) %>%
  filter(!is.na(Ozone))

It is better to encapsulate this code in a function.

read_and_clean <- function(path) {
  path %>%
    read_csv(col_types = cols()) %>%
    filter(!is.na(Ozone))
}

Now, instead of invoking a whole block of text, all you need to do is type a small reusable command. The function name speaks for itself, so you can recall what it does without having to mentally process all the details again.

read_and_clean("data/raw_data.csv")

As with the data, we can write a function to fit a model,

fit_model <- function(data) {
  biglm(Ozone ~ Wind + Temp, data)
}

and another function to plot the data.

create_plot <- function(data) {
  ggplot(data) +
    geom_histogram(aes(x = Ozone)) +
    theme_gray(24)
}

5.4 Functions in pipelines

Without those functions, our pipeline in the walkthrough chapter would look long, complicated, and difficult to digest.

# _targets.R
library(targets)
source("R/functions.R")
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("biglm", "rmarkdown", "tidyverse"))
list(
  tar_target(raw_data_file, "data/raw_data.csv", format = "file"),
  tar_target(raw_data, read_csv(raw_data_file, col_types = cols())),
  tar_target(
    data,
    raw_data %>%
      filter(!is.na(Ozone))
  ),
  tar_target(fit, biglm(Ozone ~ Wind + Temp, data)),
  tar_target(
    hist,
    ggplot(data) +
      geom_histogram(aes(x = Ozone)) +
      theme_gray(24)
  )
)

But if we write our functions in R/functions.R and source() them into the target script file (default: _targets.R) the pipeline becomes much easier to read. We can even condense out raw_data and data targets together without creating a large command.

# _targets.R
library(targets)
source("R/functions.R")
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("biglm", "tidyverse"))
list(
  tar_target(raw_data_file, "data/raw_data.csv",format = "file"),
  tar_target(data, read_and_clean(raw_data_file)),
  tar_target(fit, fit_model(data)),
  tar_target(hist, create_plot(data))
)
Copyright Eli Lilly and Company