str_glue

The function stringr::str_glue() is similar to str_c() but allows you to include variable names within double quotes.

Variables to be used are flanked by curly braces, e.g. "{variable_name}".

Scalars

You can include a string variable into a string specified in stringr::glue().

#Create a string scalar
base <- "A"
#Glue
stringr::str_glue("Adenine is represented by the letter {base}")
Adenine is represented by the letter A

You can include multiple variables.

#Create string vectors
base <- "A"
comp <- "T"
#Glue
stringr::str_glue("The complementary base of {base} is {comp}")
The complementary base of A is T

Like paste0() or str_c() you can combine multiple strings and set a separator. In this case the option is .sep=.

#Create string vectors
dna <- "T"
rna <- "U"
#Glue
stringr::str_glue("{dna} is a nucleic acid DNA base.",
                    "{rna} is a nucleic acid RNA base.",
                    .sep = " ")
T is a nucleic acid DNA base. U is a nucleic acid RNA base.

If you need to display something such as {base} you can use double braces ({{base}}).

Additionally we will use a new space (\n) as the separator.

stringr::str_glue("You can include a variable with str_glue with braces, e.g. {{base}}.",
                    "For example, here is adenine: {base}.",
                    .sep = "\n") |>
    stringr::str_view()
[1] │ You can include a variable with str_glue with braces, e.g. {base}.
    │ For example, here is adenine: A.

Non strings

Non strings are converted into strings.

#Create a numeric scalar
num <- 4
#Glue
stringr::str_glue("There are {num} DNA bases")
There are 4 DNA bases

With numerics you can carry out maths in the braces.

stringr::str_glue("There are {num/2} purines.",
                    "There are {num-1} pyrimidines",
                    .sep = "\n") |>
    stringr::str_view()
[1] │ There are 2 purines.
    │ There are 3 pyrimidines

Below shows how logicals and missing values work.

#Vectors
true <- TRUE
missing <- NA
#Glue
stringr::str_glue("Logical: {true}. Missing value: {missing}.") |>
    stringr::str_view()
[1] │ Logical: TRUE. Missing value: NA.

Vectors

Using vectors with str_glue() works the same way as str_c().

#Create vec
bases <- c("A","G","C","T")
#Glue
stringr::str_glue("A DNA base is {bases}") |> stringr::str_view()
[1] │ A DNA base is A
[2] │ A DNA base is G
[3] │ A DNA base is C
[4] │ A DNA base is T

When using multiple vectors ensure they are the same length.

#Create vec
bases <- c("A","G","C","T")
names <- c("Adenine", "Guanine", "Cytosine", "Thymine")
#Glue
stringr::str_glue("{names} is represented by {bases}") |>
    stringr::str_view()
[1] │ Adenine is represented by A
[2] │ Guanine is represented by G
[3] │ Cytosine is represented by C
[4] │ Thymine is represented by T

Using vectors of different lengths will lead to an error but you can always use scalars with vectors.

#Create vec
type <- "DNA"
#Glue
stringr::str_glue("The {type} base {names} is represented by {bases}") |>
    stringr::str_view()
[1] │ The DNA base Adenine is represented by A
[2] │ The DNA base Guanine is represented by G
[3] │ The DNA base Cytosine is represented by C
[4] │ The DNA base Thymine is represented by T