Type: Package
Title: Process Command Line Arguments
Version: 0.1.2
Description: Process command line arguments, as part of a data analysis pipeline. The pipeline is controlled by a Makefile or shell script. Functions to construct Makefiles and shell scripts are included in a the package. The aim is a pipeline that is modular, transparent, and reliable.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 3.5.0)
Imports: cli, fs, methods, tools
Suggests: covr, dplyr, ggplot2, knitr, littler, quarto, readr, rmarkdown, testthat (≥ 3.0.0), tidyr, withr
Config/testthat/edition: 3
RoxygenNote: 7.3.3
URL: https://bayesiandemography.github.io/command/, https://github.com/bayesiandemography/command
Config/Needs/website: quarto, rmarkdown
BugReports: https://github.com/bayesiandemography/command/issues
NeedsCompilation: no
Packaged: 2025-10-11 05:44:57 UTC; johnbryant
Author: John Bryant [aut, cre], Bayesian Demography Limited [cph]
Maintainer: John Bryant <john@bayesiandemography.com>
Repository: CRAN
Date/Publication: 2025-10-16 18:10:07 UTC

command: Process command line arguments

Description

Process arguments passed at the command line, as part of data analysis workflow.

Details

Author(s)

Maintainer: John Bryant john@bayesiandemography.com

Other contributors:

See Also

Useful links:


Assign Values Passed at the Command Line or Interactively

Description

Assign values to names in the working environment. The values are typically supplied through the command line, but can be supplied interactively.

Specifying the inputs and outputs of scripts through the command line can contribute to safter, more modular workflows.

cmd_assign_quiet() is identical to cmd_assign(), but does not print progress messages to the console.

Usage

cmd_assign(...)

cmd_assign_quiet(...)

Arguments

...

Name-value pairs.

Value

cmd_assign() is called for its side effect, which is to create objects in the global environment. However, cmd_assign() also invisibly returns a named list of objects.

Types of session

cmd_assign() behaves differently depending on how it whether it is called

  1. interactively, or

  2. inside an R script that is run from the command line.

For instance, if the code

cmd_assign(use_log = TRUE)

is run interactively, it creates an object called use_log with value TRUE.

But if the same code is run inside a script via the command

⁠Rscript tidy_data.R --use_log=FALSE⁠

it creates an object called use_log with value FALSE.

cmd_assign() is typically called interactively when a workflow is being developed, and through the command line when the workflow has matured.

Matching names and values

When used in a script called from the command line, cmd_assign() first matches named command line arguments, and then matches unnamed command line arguments, in the order in which they are supplied.

If, for instance, the script person.R contains the lines

cmd_assign(.data = "raw_data.csv",
           max_age = 85,
           .out = "person.rds")

and if person.R is run from the command line using

Rscript person.R raw_data.csv person.rds --max_age=100

then cmd_assign() first matches named command line argument --max_age=100 to ⁠cmd_assign() argument ⁠max_age⁠, and then matches unnamed command line arguments ⁠raw_data.csvandperson.rdstocmd_assign()arugments.dataand.out'.

Coercing values passed at the command line

Values passed at the command line start out as text strings. cmd_assign() coerces these text strings to have the same class as the corresponding values in the call to cmd_assign(). For instance, if a script called fit.R contains the lines

cmd_assign(.data = "cleaned.rds",
           impute = TRUE,
           date = as.Date("2026-01-01"),
           .out = "fit.rds")

and if fitted.R is run from the command line using

Rscript fitted.R cleaned.rds fit.rds --impute=TRUE --date=2025-01-01

then cmd_assign() will create

References

See Also

Examples

if (interactive()) {
  cmd_assign(.data = "mydata.csv",
             n_iter = 2000,
             .out = "results.rds")
}

Turn a 'cmd_assign' Call Into a Makefile Rule

Description

Extract a call to cmd_assign() from an R script, and turn it into a Makefile rule.

Usage

extract_make(path_file, dir_make = NULL)

Arguments

path_file

A path from dir_make to the R scripe containing the call to cmd_assign().

dir_make

The directory that contains the Makefile. The default is the current working directory.

Value

extract_make() is typically called for its side effect, which is to print a Makefile rule. However, extract_make() invisibly returns a text string with the rule.

The components of a Makefile rule

A Makefile rule produced by extract_make() normally looks something like this:

out/model.rds: src/model.R \
  data/cleaned.rds
       Rscript $^ $@ --use_log=TRUE

In this rule

Using extract_make() to build a data analysis workflow

When using extract_make(), it is a good idea to set the current working directory to the project directory (something that will happen automatically if you are using RStudio projects.)

Location of the Makefile

The Makefile normally sets at the top of the project, so that the project folder looks something like this:

Makefile
- data/
- src/
- out/
report.qmd

Identifying file arguments

To construct the Makefile rule, extract_make() needs to be able to pick out arguments that refer to file names. To do so, it uses the following heuristic:

References

See Also

Examples

library(fs)
library(withr)
with_tempdir({

  ## create 'src'  directory
  dir_create("src")

  ## put an R script containing a call 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")

  ## call 'extract_make()'
  extract_make(path_file = "src/results.R",
               dir_make = ".")

})

Turn a 'cmd_assign' Call Into a Shell Command

Description

Extract a call to cmd_assign() from an R script, and turn it into a shell command.

Usage

extract_shell(path_file, dir_shell = NULL)

Arguments

path_file

Path to the R script containing the call to cmd_assign(). The path starts at dir_shell.

dir_shell

The directory that contains the shell script. The default is the current working directory.

Value

extract_shell() is typically called for its side effect, which is to print a shell command. However, extract_shell() invisibly returns a text string with the command.

The components of a shell command

The shell command produced by extract_shell() normally looks something like this:

Rscript src/model.R \
  data/cleaned.rds \
  out/model.rds \
  --use_log=TRUE

In this command

Using extract_shell() to build a data analysis workflow

Location of the shell script

The shell script normally sits at the top level of the project, so that the project folder looks something like this:

workflow.sh
- data/
- src/
- out/
report.qmd

Identifying file arguments

To construct the rule, extract_shell() needs to be able to identify arguments that refer to a file name. To do so, it uses the following heuristic:

References

See Also

Examples

library(fs)
library(withr)

with_tempdir({

  ## create 'src' directory
  dir_create("src")

  ## add an R script containing a call to 'cmd_assign'
  writeLines(c("cmd_assign(x = 1, .out = 'out/results.rds')",
               "results <- x + 1",
               "saveRDS(results, file = .out)"),
             con = "src/results.R")

  ## call 'extract_shell()'
  extract_shell(path_file = "src/results.R",
                dir_shell = ".")

})

Create a Makefile

Description

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_make to a directory with R scripts containing calls to cmd_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.

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.

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.

References

See Also

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")

})

Create a Shell Script

Description

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_shell to a directory with R scripts containing calls to cmd_assign().

dir_shell

The directory where shell_script() will create the shell script. If no value is supplied, then shell_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.

Details

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.

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.

References

See Also

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 'shell_script()'
  shell_script(path_files = "src",
               dir_shell = ".")

  ## shell script has been created
  dir_tree()

  ## print contents of shell script
  cat(readLines("workflow.sh"), sep = "\n")

})