glue glue() in R: Interpolate Variables Into Strings
glue glue() builds a string by interpolation: you write the finished sentence as a template and mark each variable slot with {braces}. Every expression inside the braces is evaluated as real R code and its result is inserted in place. It is the readable alternative to stacking many paste() or sprintf() arguments.
glue("Hi {x}", x = "Ada") # "Hi Ada" interpolate a value
glue("2 + 2 = {2 + 2}") # "2 + 2 = 4" evaluate an expression
glue("Upper: {toupper('ada')}") # "Upper: ADA" call a function
glue("a", "b", "c") # "abc" join plain strings
glue("Show {{x}} literally") # "Show {x} literally" escape braces
glue("user_{1:3}") # length-3 glue vector, vectorised
glue("{a} of {b}", a = 3, b = 10) # "3 of 10" multiple named valuesNeed explanation? Read on for examples and pitfalls.
What glue() does in one sentence
glue() evaluates R expressions wrapped in {braces} and inserts the results into a template string. You write the sentence exactly as it should read, leave a brace-marked hole wherever a value belongs, and glue() fills each hole by running the code inside it.
This is string interpolation: the template and the output have the same shape, so what you write is what you get. It is the readable counterpart to paste(), which joins separate pieces rather than filling a template.
The {name} slot is replaced with the value of name, producing one finished sentence.
Syntax
glue(..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}") builds a string from templates and values. The ... arguments are the template strings, .sep joins them when you pass more than one, .envir is the environment where each {expr} is evaluated, and .open / .close change the delimiter pair.
Anything inside {} is treated as R code, not just a variable name. That means you can call functions and run arithmetic directly in the template.
Both {x} and {x^2} are evaluated, so the second slot computes 25 on the fly.
Five common glue() scenarios
Five scenarios cover almost every real use of glue(). Each block stands alone, so you can paste it straight into the live console.
Interpolate variables into a sentence
The core job of glue() is dropping variables into a readable sentence. Each {} slot names the variable that belongs there.
The $ is a literal character; only the {price} slot is interpolated.
Evaluate expressions inside braces
Braces hold any R expression, not just bare names. You can call functions and the result is inserted directly.
mean(values) and max(values) both run before the string is assembled.
Interpolate a vector to get many strings
When a brace holds a vector, glue() returns one string per element. The template is recycled across every value.
The single template produces a length-three result, one string for each id.
Pass values as named arguments
Named arguments become variables for the template. This keeps a value local to the call instead of relying on the surrounding environment.
city and country exist only inside this glue() call.
Use glue inside a mutate pipeline
Most interpolation in real code happens inside a tidyverse pipeline. Pass columns to glue() inside mutate() to build a label column.
glue() is vectorised, so it fills the template once per row.
glue() vs paste() vs sprintf()
Three functions assemble strings from values, and they differ in template style. The choice comes down to how much formatting control you need.
All three produce the same text. glue() keeps the value inline with the words, paste0() alternates literal and variable pieces, and sprintf() uses % placeholder codes.
| Function | Template style | Best for |
|---|---|---|
glue() |
{expr} inline |
readable sentences with variables |
paste0() |
alternating pieces | quick joins of a few values |
sprintf() |
%d, %.2f codes |
precise numeric formatting |
Reach for glue() when the output is a sentence a human reads, and sprintf() when a number needs an exact width or decimal count.
{1/3} becomes a long decimal. To control decimal places, format inside the braces with {round(x, 2)} or {format(x, nsmall = 2)}. For strict numeric formatting, switch to sprintf("%.2f", x) and wrap that result in the glue template if needed.Common pitfalls
Three pitfalls cause most glue() surprises. Each has a one-line fix.
A missing variable raises an error
Any name inside braces must exist, or glue() stops. Because the braces hold real code, an undefined name is the same "object not found" error you see anywhere else.
Define the variable first, or pass it as a named argument to the call.
Now the name resolves inside the call and the template fills cleanly.
Literal braces need doubling
A single brace is always treated as code, never as text. To print a real { or }, double it.
{{ collapses to one literal {, and }} to one literal }, so the braces survive into the output. If your template already uses braces (for example in LaTeX), switch the delimiters with .open = "<<" and .close = ">>".
glue returns a glue object, not a plain string
glue() returns an object of class glue, not a bare character vector. Most code treats it like a string, but strict type checks can trip on the extra class.
Wrap the result in as.character() whenever a function or test demands a plain character vector.
identical(glue("ab"), "ab") returns FALSE because the class attribute differs, even though the text matches. When a comparison or unit test fails unexpectedly, convert with as.character() before checking equality.Try it yourself
Try it: Use glue() to build the sentence "Tokyo has 14 wards." from the variables city and wards. Save the result to ex_sentence.
Click to reveal solution
Explanation: glue() evaluates each {} expression against the current environment, so {city} becomes "Tokyo" and {wards} becomes 14. Both results are inserted into the template in the order they appear.
Related glue functions
When glue() is not quite what you need, these are the next stops:
- glue_data() interpolates from a data frame or list, which is cleaner inside a pipeline.
- glue_collapse() collapses a vector into a single string with a separator.
- glue_sql() quotes values safely for SQL statements sent through DBI.
- str_c() joins fixed pieces element-wise when there is no template to fill.
- str_glue() is the stringr re-export of glue() with the same syntax.
- The full glue package reference documents glue() and its arguments.
FAQ
What is the difference between glue() and paste() in R?
They differ in template style. paste() alternates literal strings and variables as separate arguments, while glue() takes one template string with {} slots where values belong. glue("Hi {name}") reads like the finished sentence, whereas paste0("Hi ", name) splits it into pieces. glue() also evaluates real R code inside the braces, so {x^2} or {toupper(x)} works directly in the template, and the result is vectorised across any vector you reference.
How do I use a variable inside glue()?
Wrap the variable name in curly braces inside the template: glue("Hello, {name}") inserts the value of name. The variable must exist in the calling environment, or you can supply it as a named argument, such as glue("Hello, {name}", name = "Sam"). Anything inside the braces is run as R code, so you can also call functions, like glue("{toupper(name)}").
How do I print literal curly braces with glue()?
Double the brace. {{ produces one literal { and }} produces one literal } in the output. For example, glue("A set {{1, 2}}") returns A set {1, 2}. For templates with many braces (LaTeX, JSON), switch delimiters with .open = "<<" and .close = ">>".
Is glue() the same as str_glue()?
Almost. str_glue() is a thin wrapper around glue() re-exported by stringr. The arguments and brace syntax are identical, and both return a glue object. Pick whichever package is already loaded.
Can glue() interpolate data frame columns?
Yes, but glue_data() is the cleaner choice for that job. It takes a data frame or list as its first argument and evaluates {} slots against its columns, so glue_data(df, "{name}: {score}") reads each column by name. Inside a mutate() call, plain glue() also works because the column names are already in scope as vectors.