Thestrs_format function provides string interpolation using values from a data argument, similar to Python'sstr.format method. For more information, see the details.
Arguments
- string
A character vector containing the string with placeholders in curly braces
{}. If the vector containsmore than one string, they are concatenated before interpolation.- ...
Named arguments that will be made available during
stringinterpolation. Note that these will takeprecedence over values in.data.- .data
List-like (e.g., data.frame, list, environment, etc.) object that will be used for lookups during
stringinterpolation.
Details
Under the hood, this function uses theglue::glue_data() function to perform the interpolation. Bydefault, this function will only use the variables provided in... and.data. It will NOT use the immediateenvironment to perform the interpolations. If any interpolated expression is NA, it will be NA in the output. If anyinterpolated expression is NULL, it will be NULL in the output.
Examples
# Using a named list for substitutionsdata_list<-list(name="Alice", age=30)strs_format("My name is {name} and I am {age} years old.", .data=data_list)#> [1] "My name is Alice and I am 30 years old."# Output: "My name is Alice and I am 30 years old."# Using additional argumentsstrs_format("My name is {name} and I am {age} years old.", name="Bob", age=25)#> [1] "My name is Bob and I am 25 years old."# Output: "My name is Bob and I am 25 years old."