Assign Varying Set of Values Passed at the Command Line or Interactively
Source:R/cmd_assign.R
cmd_assign_collect.RdAssign names to values in the global environment,
like cmd_assign(), but without specifying the number
of values in advance. Values for unnamed command
line arguments are collected into a single character vector.
cmd_assign_collect_quiet() is identical to
cmd_assign_collect() except that it does not print
progress messages to the console.
Value
cmd_assign_collect() is called
for its side effect, which is to create objects
in the global environment. However, cmd_assign_collect()
also invisibly returns a named list of objects.
Matching names and values
cmd_assign_collect(), like cmd_assign(),
starts by processing named command line arguments
(i.e. arguments of the form --n_draw=100).
Unlike cmd_assign(), however, cmd_assign_collect()
puts all the values that are left over into a single
character vector. This vector can be any length,
including zero. cmd_assign_collect() must
contain a single name-value pair that is not
used for named command line arguments.
cmd_assign_collect() assigns the name from
this name-value to the character vector.
If, for instance, the script combine.R contains
and if combine.R is run from the command line using
then use_log will be assigned the value TRUE
and .files will be assigned the value
c("data/a.csv", "data/c.csv", "out/combined.rds").
Processing the vector of unnamed arguments
The R script in which cmd_assign_collect() is
called has responsibility for processing the
character vector of values from unnamed command
line arguments. The call to cmd_assign_collect() above,
for instance, might be followed by statements such as
Use cmd_assign() where possible
cmd_assign_collect() is more flexible than cmd_assign(),
in that it does not require the number, name, or type of values
to be specified in advance. But if these things
are in fact known in advance, then it is
good practice to document and test for them,
which is what cmd_assign() does.
See also
cmd_assign()Process command line arguments with one value per argumentextract_shell()Turn acmd_assign()orcmd_assign_collect()call into a shell commandextract_make()Turn acmd_assign()orcmd_assign_collect()call into a Makefile ruleshell_script()Create a shell scriptmakefile()Create a MakefileQuick Start How to use
cmd_assign()
Examples
if (interactive()) {
cmd_assign_collect(
files = c("data/x.csv", "data/y.csv"),
use_log = TRUE
)
}