The strs_f
function allows for inline variable interpolation in strings, similar to Python's f-strings.
Arguments
- string
A character vector containing the string with placeholders in curly braces
{}
. If the vector contains more than one string, they are concatenated before interpolation.
Details
Under the hood, this function uses the glue::glue_data()
function to perform the interpolation. However,
this function doesn't let you change the evaluation context or perform any string trimming. If any interpolated
expression is NA, it will be NA in the output. If any interpolated expression is NULL, it will be NULL in the output.
Examples
name <- "Alice"
age <- 30
strs_f("My name is {name} and I am {age} years old.")
#> [1] "My name is Alice and I am 30 years old."
# Output: "My name is Alice and I am 30 years old."
age <- NULL
strs_f("My name is {name} and I am {age} years old.")
#> [1] "My name is Alice and I am NULL years old."
# Output: "My name is Alice and I am NULL years old."