Build Rules

Michal Burda

2026-01-08

Introduction

This vignette describes the various types of build rules available in rmake for defining how targets are built from their dependencies.

For information on project management (initialization, running builds, cleaning), see the rmake Project Management vignette. For advanced features like tasks and templates, see the Tasks and Templates vignette.

Common Rule Parameters

All rule functions have the following common parameters:

Each rule executes in a separate R process (no shared state).

Note: The task parameter is covered in detail in the Tasks and Templates vignette, and the params parameter is explained in the parameterized execution section of that vignette.

Pre-defined Rule Types

rRule()

Executes an R script using Rscript. The rule is triggered when any dependency or the script itself changes.

rRule(target, script, depends = NULL, params = list(), task = "all")

Parameters:

Example:

rRule(target = "output.rds", 
      script = "process.R", 
      depends = "input.csv")

markdownRule()

Renders a document from R Markdown using rmarkdown::render().

markdownRule(target, script, depends = NULL, format = "all",
             params = list(), task = "all")

Parameters:

Format Options:

Example:

markdownRule(target = "report.pdf",
             script = "report.Rmd",
             depends = "data.rds",
             format = "pdf_document")

knitrRule()

Executes knitr to create a text file using knitr::knit(). This is useful for processing Sweave-style documents (.Rnw files).

knitrRule(target, script, depends = NULL, params = list(), task = "all")

Parameters:

Example:

knitrRule(target = "report.tex",
          script = "report.Rnw",
          depends = c("data1.csv", "data2.csv"))

copyRule()

Copies a file from one location to another. The rule executes $(CP) depends[1] target.

copyRule(target, depends, task = "all")

Parameters:

Example:

copyRule(target = "backup/data.csv",
         depends = "data.csv")

depRule()

Defines a dependency between targets without providing any execution script. This is useful when you want to specify that a target depends on another target but don’t need to execute any command to build it.

depRule(target, depends = NULL, task = "all")

Parameters:

Example:

# Ensure all preprocessing is done before starting the analysis
depRule(target = "analysis-ready",
        depends = c("data1.rds", "data2.rds", "data3.rds"))

subdirRule()

Runs the make process in a subdirectory. The subdirectory is assumed to contain its own Makefile. This rule executes make <targetTask> in the specified subdirectory.

subdirRule(target, depends = NULL, task = "all", targetTask = "all")

Parameters:

Example:

subdirRule(target = "subproject",
           targetTask = "all")

offlineRule()

Forces manual action within the build process. Shows a custom error message instructing the user to perform a task manually. This is useful when transformation requires manual intervention.

offlineRule(target, message, depends = NULL, task = "all")

Parameters:

Example:

offlineRule(target = "cleaned_data.csv",
            message = "Please manually clean data.csv and save as cleaned_data.csv",
            depends = "data.csv")

Custom Rules

Create custom rules using the general rule() function:

rule(target, depends = NULL, build = NULL, clean = NULL,
     task = "all", phony = FALSE)

Arguments:

Predefined Make Variables:

Example with NodeJS:

r <- rule(target = "test.json", 
          depends = "test.js", 
          build = "node test.js",
          clean = "$(RM) test.json")

Define custom Make variables:

defaultVars["JS"] <- "/usr/bin/node"

job <- list(rule(target = "test.json",
                 depends = "test.js",
                 build = "$(JS) test.js",
                 clean = "$(RM) test.json"))

Using inShell()

The inShell() function converts R expressions to shell commands:

inShell({ result <- 1 + 1; saveRDS(result, "result.rds") })
#> [1] "$(R) - <<'EOFrmake'"                 "{"                                  
#> [3] "    result <- 1 + 1"                 "    saveRDS(result, \"result.rds\")"
#> [5] "}"                                   "EOFrmake"

Example rule using inShell():

rule(target = "result.rds",
     build = inShell({ result <- 1 + 1; saveRDS(result, "result.rds") }),
     clean = "$(RM) result.rds")

Note: Overuse of inShell() is not recommended. Prefer separate script files so Make can detect changes.

Summary

rmake provides a comprehensive set of rule types:

See Also

For more information on related topics, see these vignettes: