knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5 ) library(rmake)
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.
All rule functions have the following common parameters:
target - Character vector of files to createdepends - Character vector of prerequisite files (optional)task - Task name(s) for grouping (default: "all")params - Parameters to pass to scripts (optional for some rules)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.
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:
target - Name of the output file to be createdscript - Name of the R script to executedepends - Vector of file names that the script depends on, or NULLparams - List of R values that become available within the script in a params variabletask - Character vector of parent task namesExample:
rRule(target = "output.rds", script = "process.R", depends = "input.csv")
Renders a document from R Markdown using rmarkdown::render().
markdownRule(target, script, depends = NULL, format = "all", params = list(), task = "all")
Parameters:
target - Name of the output file to be createdscript - Name of the R Markdown file to renderdepends - Vector of file names that the markdown script depends on, or NULLformat - Output format specification (see below)params - List of R values available within the script in a params variabletask - Character vector of parent task namesFormat Options:
"all" - All formats defined in the Rmd file"html_document" - HTML web page"pdf_document" - PDF document"word_document" - Microsoft Word"odt_document" - OpenDocument Text"rtf_document" - Rich Text Format"md_document" - MarkdownExample:
markdownRule(target = "report.pdf", script = "report.Rmd", depends = "data.rds", format = "pdf_document")
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:
target - Name of the output file to be createdscript - Name of the Rnw file to be rendereddepends - Vector of file names that the knitr script depends on, or NULLparams - List of R values available within the script in a params variabletask - Character vector of parent task namesExample:
knitrRule(target = "report.tex", script = "report.Rnw", depends = c("data1.csv", "data2.csv"))
Copies a file from one location to another. The rule executes $(CP) depends[1] target.
copyRule(target, depends, task = "all")
Parameters:
target - Target file name to copy the file todepends - Name of the file to copy from (only the first element is used)task - Character vector of parent task namesExample:
copyRule(target = "backup/data.csv", depends = "data.csv")
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:
target - Target file name that depends on dependsdepends - Character vector of prerequisite file namestask - Character vector of parent task namesExample:
# Ensure all preprocessing is done before starting the analysis depRule(target = "analysis-ready", depends = c("data1.rds", "data2.rds", "data3.rds"))
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:
target - Name of the subdirectorydepends - Must be NULLtask - Character vector of parent task namestargetTask - What task to execute in the subdirectory (default: "all")Example:
subdirRule(target = "subproject", targetTask = "all")
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:
target - Name of the file to be created manuallymessage - Custom message to display to the userdepends - Vector of prerequisite file names, or NULLtask - Character vector of parent task namesExample:
offlineRule(target = "cleaned_data.csv", message = "Please manually clean data.csv and save as cleaned_data.csv", depends = "data.csv")
Create custom rules using the general rule() function:
rule(target, depends = NULL, build = NULL, clean = NULL, task = "all", phony = FALSE)
Arguments:
target - Target file namesdepends - Prerequisite file namesbuild - Shell commands to build targetsclean - Shell commands to clean targetstask - Task assignmentphony - Whether target is a non-file target (TRUE/FALSE)Predefined Make Variables:
$(R) - Path to Rscript binary$(RM) - File deletion command$(CP) - File copy commandExample 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"))
The inShell() function converts R expressions to shell commands:
inShell({ result <- 1 + 1; saveRDS(result, "result.rds") })
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.
rmake provides a comprehensive set of rule types:
For more information on related topics, see these vignettes:
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.