Create a Makefile for a data analysis workflow. The Makefile can include rules extracted from existing R files.
Usage
makefile(
path_files = NULL,
dir_make = NULL,
name_make = "Makefile",
overwrite = FALSE,
quiet = FALSE
)Arguments
- path_files
A path from
dir_maketo a directory with R scripts containing calls tocmd_assign(). Optional.- dir_make
The directory where
makefile()will create the Makefile. If no value is supplied, then `makefile(); creates the Makefile the current working directory.- name_make
The name of the Makefile. The default is
"Makefile".- overwrite
Whether to overwrite an existing Makefile. Default is
FALSE.- quiet
Whether to suppress progress messages. Default is
FALSE.
Value
makefile() is called for its
side effect, which is to create a
file. However, makefile() also
returns a string with the contents of the
Makefile.
Details
To create a Makefile in the files
directory, set files to ".".
To obtain the contents of the Makefile
without creating a file on disk,
creating the file on disk, set
name_make to NULL.
Supplying a value for files is
optional for makefile(),
but compulsory for shell_script().
The output from makefile()
includes some general-purpose Makefile
commands, while the output from
shell_script() is generated entirely
from files.
References
Project Management with Make Makefiles in data analysis workflows
GNU make Definitive guide
Command-Line Programs Introduction to Rscript
See also
Creating a Makefile More on
makefile()extract_make()Turn acmd_assign()call into a Makefile ruleshell_script()Shell script equivalent ofmakefile()cmd_assign()Process command line argumentsModular Workflows for Data Analysis Safe, flexible data analysis workflows
littler Alternative to Rscript
Examples
library(fs)
library(withr)
with_tempdir({
## create 'src' directory
dir_create("src")
## put R scripts containing calls to
## 'cmd_assign' in the 'src' directory
writeLines(c("cmd_assign(x = 1, .out = 'out/results.rds')",
"results <- x + 1",
"saveRDS(results, file = .out)"),
con = "src/results.R")
writeLines(c("cmd_assign(x = 1, .out = 'out/more_results.rds')",
"more_results <- x + 2",
"saveRDS(more_results, file = .out)"),
con = "src/more_results.R")
## call 'makefile()'
makefile(path_files = "src",
dir_make = ".")
## Makefile has been created
dir_tree()
## print contents of Makefile
cat(readLines("Makefile"), sep = "\n")
})
#> ✔ Extracted call to `cmd_assign()` in src/more_results.R.
#> ✔ Extracted call to `cmd_assign()` in src/results.R.
#> .
#> ├── Makefile
#> └── src
#> ├── more_results.R
#> └── results.R
#>
#> .PHONY: all
#> all:
#>
#>
#> out/more_results.rds: src/more_results.R
#> Rscript $^ $@ --x=1
#>
#> out/results.rds: src/results.R
#> Rscript $^ $@ --x=1
#>
#>
#> .PHONY: clean
#> clean:
#> rm -rf out
#> mkdir out
#>
#>