Learn R Programming

makeit (version 1.0.2)

make: Run R Script if Needed

Description

Run an R script if underlying files have changed, otherwise do nothing.

Usage

make(recipe, prereq, target, include = TRUE, details = FALSE,
  force = FALSE, recon = FALSE, quiet = FALSE, ...)

Value

TRUE or FALSE, indicating whether the script was run.

Arguments

recipe

script filename.

prereq

one or more files required by the script. For example, data files, scripts, or NULL.

target

one or more output files produced by the script. Directory names can also be used.

include

whether to automatically include the script itself as a prerequisite file. This means that if the script file has been modified, it should be run.

details

whether to show a diagnostic table of files and time last modified.

force

whether to run the R script unconditionally.

recon

whether to return TRUE or FALSE, without actually running the R script.

quiet

whether to suppress messages.

...

passed to source.

Details

A make() call has the general form


make("analysis.R", "input.dat", "output.dat")

which can be read aloud as:

“script x uses y to produce z

References

Stallman, R. M. et al. An introduction to makefiles. Chapter 2 in the GNU Make manual.

See Also

See vignette("makeit") for annotated examples and discussion.

file.exists and file.mtime are the underlying functions used to check if files are missing or have changed.

source is the underlying function to run a script.

Examples

Run this code
# \donttest{
# Copy examples 'analysis' and 'sequential' to temporary directory
exdir <- tempdir()
file.copy(system.file("examples/analysis", package="makeit"),
          exdir, recursive=TRUE)
file.copy(system.file("examples/sequential", package="makeit"),
          exdir, recursive=TRUE)
owd <- getwd()

# This analysis uses input.dat to produce output.dat
setwd(file.path(exdir, "analysis"))
dir()
make("analysis.R", "input.dat", "output.dat")  # Running analysis.R
dir()
make("analysis.R", "input.dat", "output.dat")  # Nothing to be done

# Suppress message, show last modified
make("analysis.R", "input.dat", "output.dat", quiet=TRUE)
make("analysis.R", "input.dat", "output.dat", details=TRUE)

# Sequential scripts
setwd(file.path(exdir, "sequential"))
print.simple.list(dir(recursive=TRUE))
make("01_model.R", "data.dat", "results.dat")
make("02_plots.R", "results.dat", c("plots/A.png", "plots/B.png"))
make("03_tables.R", "results.dat", c("tables/A.csv", "tables/B.csv"))
print.simple.list(dir(recursive=TRUE))

# Clean up
unlink(file.path(exdir, c("analysis", "sequential")), recursive=TRUE)
setwd(owd)

# See vignette("makeit") for more examples and discussion
# }

Run the code above in your browser using DataLab