Create a shell script for a data analysis workflow consisting of commands extracted from existing R files.
Usage
shell_script(
path_files,
dir_shell = NULL,
name_shell = "workflow.sh",
overwrite = FALSE,
quiet = FALSE
)Arguments
- path_files
A path from
dir_shellto a directory with R scripts containing calls tocmd_assign()orcmd_assign_collect().- dir_shell
The directory where
shell_script()will create the shell script. If no value is supplied, thenshell_script()creates the shell script in the current working directory.- name_shell
The name of the shell script. The default is
"workflow.sh".- overwrite
Whether to overwrite an existing shell script. Default is
FALSE.- quiet
Whether to suppress progress messages. Default is
FALSE.
Value
shell_script() is called for its
side effect, which is to create a
file. However, shell_script() also
returns a string with the contents of the
shell script.
Details
shell_script() tries to put the commands in the order
they need to run, by matching each script's output path
with the input paths of the other scripts.
It can get this order wrong, so the shell script may
still need editing.
To create a shell script in the files
directory, set files to ".".
To obtain the contents of the shell script
without creating a file on disk,
creating the file on disk, set
name_shell to NULL.
Supplying a value for files is
compulsory for shell_script(),
but optional for makefile().
The output from shell_script()
is generated entirely from files
while the output from makefile()
also includes some general-purpose Makefile
commands.
References
Episodes 1–3 of The Unix Shell Introduction to the command line
Command-Line Programs Introduction to Rscript
See also
Creating a Shell Script More on
shell_script()extract_shell()Turn acmd_assign()call into a shell commandmakefile()Makefile equivalent ofshell_script()cmd_assign()Process command line argumentsModular Workflows for Data Analysis Safe, flexible data analysis workflows
littler Alternative to Rscript
Examples
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(.results = 'out/results.rds', .out = 'out/more_results.rds')",
"results <- readRDS(.results)",
"more_results <- results + 2",
"saveRDS(more_results, file = .out)"),
con = "src/more_results.R")
## the shell script runs 'results.R' first, because
## 'more_results.R' reads the file that 'results.R' writes
## call 'shell_script()'
shell_script(path_files = "src",
dir_shell = ".")
## shell script has been created
list.files()
## print contents of shell script
cat(readLines("workflow.sh"), sep = "\n")
})
#> ✔ Extracted call to `cmd_assign()` in src/more_results.R.
#> ✔ Extracted call to `cmd_assign()` in src/results.R.
#>
#> Rscript src/results.R \
#> out/results.rds \
#> --x=1
#>
#> Rscript src/more_results.R \
#> out/results.rds \
#> out/more_results.rds
#>
#>