glue()
Installation
Install the glue package from CRAN:
install.packages("glue")
The package is part of the tidyverse ecosystem and requires R 3.6.0 or later.
Basic Usage
The glue() function formats and interpolates strings, evaluating R expressions inside braces {...}. Unnamed arguments are strings to format; named arguments become temporary variables available within the braces.
name <- "Fred"
age <- 50
glue("My name is {name} and I am {age} years old.")
# My name is Fred and I am 50 years old.
Multiple arguments are concatenated using the .sep parameter, which defaults to an empty string:
glue("My name is ", name, " and I am ", age, " years old.")
# My name is Fred and I am 50 years old.
Named arguments create temporary variables scoped to the glue call:
glue("Hello {person}!", person = "Alice")
# Hello Alice!
Expressions inside braces are evaluated as R code:
x <- 10
y <- 3
glue("{x} + {y} = {x + y}")
# 10 + 3 = 13
Handling NA and NULL
By default, NA values are replaced with the string "NA":
x <- NA
glue("Value is {x}")
# Value is NA
To propagate NA values (keep them as R’s logical NA), set .na = NULL:
x <- NA
glue("Value is {x}", .na = NULL)
# NA (logical NA, not the string "NA")
Similarly, NULL values are replaced with an empty string by default:
x <- NULL
glue("Value is {x}")
# Value is
To propagate NULL values as NA, use .null = NULL:
x <- NULL
glue("Value is {x}", .null = NULL)
# NA
Escaping and Custom Delimiters
To include literal braces in output, double them:
name <- "Fred"
glue("My name is {name}, not {{name}}.")
# My name is Fred, not {name}.
For formats that use braces heavily (such as LaTeX), provide alternative delimiters with .open and .close:
x <- 42
glue("Value is <<x>>.", .open = "<<", .close = ">>")
# Value is 42.
Text after # on a line is treated as a comment and ignored. Control the comment character via the .comment parameter:
glue("Hello # this is ignored")
# Hello
glue("Hello # this is now literal", .comment = NULL)
# Hello # this is now literal
Use .literal = TRUE to treat quotes, backticks, and comments as literal characters.
Trimming Whitespace
By default, leading and trailing whitespace is trimmed from each string piece. Use .trim = FALSE to preserve whitespace:
glue(" Hello {name} ", .trim = FALSE)
# Hello Fred
Environment and Data Context
By default, expressions are evaluated in the calling environment. The .envir parameter accepts an environment, list, or data frame to lookup values from:
glue("{x} + {y} = {x + y}", .envir = list(x = 2, y = 3))
# 2 + 3 = 5
For lists and data frames in pipelines, use glue_data() which accepts list-like objects:
glue_data(list(x = 2, y = 3), "{x} + {y} = {x + y}")
# 2 + 3 = 5
In dplyr pipelines, glue_data() works with the pipe:
library(dplyr)
library(glue)
head(mtcars) %>%
mutate(description = glue_data(., "{rownames(.)} has {hp} hp")) %>%
pull(description)
# [1] "Mazda RX4 has 110 hp" "Mazda RX4 Wag has 110 hp"
# [3] "Datsun 710 has 93 hp" "Hornet 4 Drive has 110 hp"
# [5] "Hornet Sportabout has 175 hp" "Valiant has 105 hp"
Related Functions
-
glue_data(.x, ...): Likeglue()but takes a list, data frame, or environment as the first argument, with values looked up before falling back to.envir. -
glue_collapse(x, sep = ", ", last = " and "): Collapses a character vector into a single string with proper Oxford comma handling. Returns an empty glue object for zero-length input. -
glue_safe()/glue_data_safe(): Safer variants that do not execute arbitrary code—only look up values viaget(). Useful in Shiny apps where user input cannot be trusted. -
glue_sql()/glue_data_sql()/glue_sql_collapse(): For safely building SQL queries, with automatic quoting of character values and identifiers. Character results are automatically quoted, and identifiers (surrounded by backticks) are handled appropriately. -
glue_col()/glue_data_col(): Likeglue()andglue_data()but support ANSI terminal styling via the crayon package.
See Also
- stringr::str_length() — measure string length
- unite — combine columns into a single string
- dplyr::mutate() — create new columns in data frames