This rule executes R scripts to create various file outputs.
Usage
rRule(
target,
script,
depends = NULL,
params = list(),
task = "all",
preBuild = NULL,
postBuild = NULL
)Arguments
- target
Name of output files to be created
- script
Name of the R script to be executed
- depends
A vector of file names that the R script depends on, or
NULL.- params
A list of R values that become available within the
scriptin aparamsvariable.- task
A character vector of parent task names. The mechanism of tasks allows grouping rules. Anything different from
'all'will cause the creation of a new task depending on the given rule. Executingmake tasknamewill then force building this rule.- preBuild
A character vector of shell commands to be executed before building the target
- postBuild
A character vector of shell commands to be executed after building the target
Details
In detail, this rule executes the following command in a separate R process:
params <- params; source(script)
That is, the parameters given in the params argument are stored in the global variable
and then the script is sourced. Note that the re-generation of the Makefile with any change
to params will not cause the re-execution of the recipe unless other script dependencies change.
Issuing make clean from the shell causes removal of all files specified in the target parameter.
Examples
r <- rRule(target='cleandata.csv',
script='clean.R',
depends=c('data1.csv', 'data2.csv'))
# generate the content of a makefile (as character vector)
makefile(list(r))
#> [1] "# This file is generated by rmake - do not edit by hand"
#> [2] "ifeq ($(origin .FEATURES),undefined)"
#> [3] "$(error This Makefile requires GNU Make. Please install it and use 'gmake' or 'make' depending on your system)"
#> [4] "endif"
#> [5] "REQUIRED_VERSION := 3.82"
#> [6] "ifeq ($(filter $(REQUIRED_VERSION),$(firstword $(sort $(MAKE_VERSION) $(REQUIRED_VERSION)))),)"
#> [7] "$(error GNU Make $(REQUIRED_VERSION) or higher is required. You are using $(MAKE_VERSION))"
#> [8] "endif"
#> [9] "SHELL=/bin/sh"
#> [10] "R=\"$(R_HOME)/bin$(R_ARCH)/Rscript\""
#> [11] "RM=rm"
#> [12] "CP=cp"
#> [13] ".ONESHELL:"
#> [14] ""
#> [15] ".PHONY: all"
#> [16] "all: Makefile cleandata.csv"
#> [17] "\t"
#> [18] "cleandata.csv: clean.R data1.csv data2.csv"
#> [19] "\t$(R) - <<'EOFrmake'"
#> [20] "\t{"
#> [21] "\t params <- list(.target = \"cleandata.csv\", .script = \"clean.R\", .depends = c(\"data1.csv\", \"data2.csv\"), .task = \"all\")"
#> [22] "\t source(\"clean.R\")"
#> [23] "\t}"
#> [24] "\tEOFrmake"
#> [25] ".PHONY: clean"
#> [26] "clean: "
#> [27] "\t$(RM) cleandata.csv"
#> [28] "Makefile: Makefile.R"
#> [29] "\t$(R) - <<'EOFrmake'"
#> [30] "\t{"
#> [31] "\t params <- list(.target = \"Makefile\", .script = \"Makefile.R\", .depends = NULL, .task = \"all\")"
#> [32] "\t source(\"Makefile.R\")"
#> [33] "\t}"
#> [34] "\tEOFrmake"
# generate to file
tmp <- tempdir()
makefile(list(r), file.path(tmp, "Makefile"))