string_vec_alias {stringmagic} | R Documentation |
Efficient creation of string vectors with optional interpolation
Description
Create string vectors in multiple ways: 1) add successive string elements (like in c()
),
or 2) write a character string that will be broken with respect to commas
("hi, there"
becomes c("hi", "there")
), or 3) interpolate variables in
character strings ("x{1:2}"
becomes c("x1", "x2")
) with full access to
string_magic()
operations, or any combination of the three.
Usage
string_vec_alias(
.cmat = FALSE,
.nmat = FALSE,
.df = FALSE,
.df.convert = TRUE,
.last = NULL,
.delim = c("{", "}"),
.split = TRUE,
.protect.vars = TRUE,
.sep = NULL,
.collapse = NULL,
.namespace = NULL
)
string_vec(
...,
.cmat = FALSE,
.nmat = FALSE,
.df = FALSE,
.df.convert = TRUE,
.delim = c("{", "}"),
.envir = parent.frame(),
.split = TRUE,
.protect.vars = TRUE,
.sep = NULL,
.last = NULL,
.collapse = NULL,
.namespace = NULL
)
stvec(
...,
.cmat = FALSE,
.nmat = FALSE,
.df = FALSE,
.df.convert = TRUE,
.delim = c("{", "}"),
.envir = parent.frame(),
.split = TRUE,
.protect.vars = TRUE,
.sep = NULL,
.last = NULL,
.collapse = NULL,
.namespace = NULL
)
Arguments
.cmat |
Logical scalar (default is The matrix is always filled by row. |
.nmat |
Logical scalar (default is The matrix is always filled by row. Non numbers are silently turned to NA. |
.df |
Logical scalar (default is If a character vector: it should give the column names. Note that if the vector
is of length 1, its values are comma separated (i.e. the value Note that the columns that can be converted to numeric are converted to numeric.
The other columns are in string form. Monitor this behavior with |
.df.convert |
Logical scalar, default is |
.last |
Character scalar, a function, or |
.delim |
Character vector of length 1 or 2. Default is If of length 1, it must be of the form: 1) the opening delimiter,
2) a single space, 3) the closing delimiter. Ex: [ ]: R:%20 [", "]: R:%22,%20%22 |
.split |
Logical or a character symbol, default is Ex: by default |
.protect.vars |
Logical scalar, default is |
.sep |
Character scalar or |
.collapse |
Character scalar or If so, pass the name of your package in this argument so that your function can access
the new |
.namespace |
Character scalar or |
... |
Character vectors that will be vectorized. If commas are present in the
character vector, it will be split with respect to commas and following blanks.
The vectors can contain any interpolation in the form Note, importantly, that interpolation and comma splitting are performed on "natural" vectors only.
That is: |
.envir |
An environment used to evaluate the variables in |
Details
The main objective of this function is to simplify the creation of small character vectors. By default, you can pass a character string of length 1 with values separated with commas and a character vector will be returned.
You can use interpolation using curly brackets (see string_magic()
). You can pass
values for the interpolation directly in the arguments (this is why all
arguments start with a dot).
By default character values containing commas are split with respect to the commas
to create vectors. To change this behavior, see the argument .split
.
The default of the argument .protect.vars
is FALSE
so as to avoid unwanted
comma-splitting and interpolations. The main use case of this function is
the creation of small string vectors, which can be written directly at
function call.
Customize the default of this function with string_vec_alias()
.
Value
By default this function returns a string vector, the length of which depends on the arguments.
This result can be processed with the arguments cmat
, nmat
and .df
which will
try to coerce the result into a character matrix, a numeric matrix , or a data frame, respectively.
Functions
-
string_vec_alias()
: Createstring_vec
aliases with custom defaults -
stvec()
: Alias tostring_vec
Author(s)
Laurent Berge
See Also
Other tools with aliases:
cat_magic_alias()
,
string_clean_alias()
,
string_magic()
,
string_magic_alias()
,
string_ops_alias()
Examples
# illustrating comma-splitting and interpolation
string_vec("x1, y2, z{4:5}")
# variable protection
x = "x{1:5}"
string_vec(x, "y{1:2}")
# without protection => interpolation takes place
string_vec(x, "y{1:2}", .protect.vars = FALSE)
# removing comma splitting
string_vec("Hi, said Charles.", "Hi, said {girl}.", girl = "Julia", .split = FALSE)
# changing the delimiters for interpolation
pkg = "\\usepackage[usenames,dvipsnames]{xcolor}"
string_vec("\\usepackage{.[S!graphicx, fourier, standalone]}",
pkg, .delim = ".[ ]")
#
# Customization
#
# Unhappy about the defaults? Create an alias!
# we create a "matrix generator"
matgen = string_vec_alias(.nmat = TRUE, .last = "'\n'split")
matgen("5, 4, 3
8, 6, 2")