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.
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
interactively, or
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
and if person.R
is run from the command line using
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.csvand
person.rdsto
cmd_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
and if fitted.R
is run from the command line using
then cmd_assign()
will create
a character vector called
.data
with value "cleaned.rds",a logical vector called
impute
with valueTRUE
,a date vector called
date
with value"2025-01-01"
, anda character vector called
.out
with value `"fit.rds".
References
Command-Line Programs Introduction to Rscript
See also
extract_shell()
Turn acmd_assign()
call into a shell commandextract_make()
Turn acmd_assign()
call into a Makefile ruleshell_script()
Create a shell scriptmakefile()
Create a MakefileQuick Start Guide How to use
cmd_assign()
Modular Workflows for Data Analysis Safe, flexible data analysis workflows.
Base R function
commandArgs()
uses a more general, lower-level approach to processing command line arguments. (commandArgs()
is called internally bycmd_assign()
.)littler Alternative to Rscript
Examples
if (interactive()) {
cmd_assign(.data = "mydata.csv",
n_iter = 2000,
.out = "results.rds")
}