rguides

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.

The first argument to glue() is the template string, and the variables you reference inside the braces are drawn from the calling environment. You can also pass the template as multiple separate string pieces — glue() will join them together before performing interpolation — and you can use the .sep parameter to control what character appears between each piece.

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.

Splitting the template across arguments is a convenience, but the more common pattern is to rely on variables from the surrounding scope. When you want to supply a value that is not already defined in the environment, you can pass it as a named argument directly to glue(), and it will be available inside the braces just like any other variable. This keeps the template self-contained and avoids polluting the global workspace.

Named arguments create temporary variables scoped to the glue call:

glue("Hello {person}!", person = "Alice")
# Hello Alice!

Named arguments give you an explicit way to inject values without relying on the calling environment, but the real flexibility of glue() comes from the fact that anything between braces is evaluated as a full R expression. That means you can write arithmetic, call functions, access list elements, or run any R code right inside the template string — glue() will evaluate it and insert the result as a character value.

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" so the output is always a complete character string. Set .na = NULL if you want NA to propagate as R’s logical NA — useful when the result feeds into further processing that checks for missing values. NULL inputs are replaced with an empty string by default; use .null = NULL to convert them to NA instead.

x <- NA
glue("Value is {x}")          #> "Value is NA"
glue("Value is {x}", .na = NULL)  #> NA (logical, not string)

x <- NULL
glue("Value is {x}")          #> "Value is "
glue("Value is {x}", .null = NULL)  #> NA

Escaping and custom delimiters

Double braces {{ }} produce literal braces in the output. For formats that use braces heavily — LaTeX equations, JSON templates — use .open and .close to define alternative delimiters. The .comment argument controls the comment character; set it to NULL to treat # as literal text.

name <- "Fred"
glue("My name is {name}, not {{name}}.")  #> My name is Fred, not {name}.

x <- 42
glue("Value is <<x>>.", .open = "<<", .close = ">>")  #> Value is 42.

glue("Hello # this is ignored")                 #> Hello
glue("Hello # this is now literal", .comment = NULL)  #> Hello # this is now literal

Environment and data context

Expressions are evaluated in the calling environment by default. The .envir parameter accepts a list, data frame, or environment to provide an explicit lookup context. For pipeline workflows, glue_data() takes the data as its first argument, making it compatible with dplyr’s mutate().

glue("{x} + {y} = {x + y}", .envir = list(x = 2, y = 3))

library(dplyr)
head(mtcars) |>
  mutate(description = glue_data(., "{rownames(.)} has {hp} hp")) |>
  pull(description)
#> [1] "Mazda RX4 has 110 hp"     "Mazda RX4 Wag has 110 hp" ...
  • glue_data(.x, ...): Like glue() 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 via get(). 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(): Like glue() and glue_data() but support ANSI terminal styling via the crayon package.

See also