string_magic
callTool intended for development: use get_interpolated_expr
to obtain the list of expressions
which will be interpolated in a string_magic()
call.
The function get_interpolated_vars
provides the variables instead.
get_interpolated_expr(x, parse = FALSE, delim = c("{", "}"))get_interpolated_vars(x, delim = c("{", "}"))
If the argument parse = FALSE
, the default, then this function returns a
character vector containing all the expressions that will be interpolated.
This vector can be empty if there is no interpolation.
If the argument parse = TRUE
, then a list is returned, containing the R expressions.
The function get_interpolated_vars
always return a character vector.
A character scalar for which the variables will be recovered.
For example x = "Hi {person}"
will return "person"
(the variable that will be interpolated).
Logical scalar, default is FALSE
. If TRUE
, the result is a list of R
expressions. If FALSE
(default), the result is a character vector of expressions.
Character vector of length 1 or 2. Default is c("{", "}")
. Defines
the opening and the closing delimiters for interpolation.
If of length 1, it must be of the form: 1) the opening delimiter,
2) a single space, 3) the closing delimiter. Ex: ".[ ]"
is equivalent to c(".[", "]")
.
The default value is equivalent to "{ }"
.
[ ]: R:%20 [", "]: R:%22,%20%22
get_interpolated_vars()
: Obtain the variables used in string_magic()
interpolations
Note that this function captures even deeply nested interpolations.
String operations: string_is()
, string_get()
, string_clean()
, string_split2df()
.
Chain basic operations with string_ops()
. Clean character vectors efficiently
with string_clean()
.
Use string_vec()
to create simple string vectors.
String interpolation combined with operation chaining: string_magic()
. You can change string_magic
default values with string_magic_alias()
and add custom operations with string_magic_register_fun()
.
Display messages while benefiting from string_magic
interpolation with cat_magic()
and message_magic()
.
Other tools with aliases:
cat_magic_alias()
,
string_magic()
,
string_magic_alias()
,
string_ops_alias()
,
string_vec_alias()
# let's create a simple interpolation
x = c("Ken", "Barbie")
sma_expr = "{' loves 'c ? x}. But does {' love 'c ? rev(x)}?"
string_magic(sma_expr)
# We recover the two expressions
(char = get_interpolated_expr(sma_expr))
# same with parsing
(expr = get_interpolated_expr(sma_expr, parse = TRUE))
# see the difference
eval(char[[1]])
eval(expr[[1]])
# and only the variables:
get_interpolated_vars(sma_expr)
Run the code above in your browser using DataLab