
glue offers interpreted string literals that are small, fast, anddependency-free. glue does this by embedding R expressions in curlybraces, which are then evaluated and inserted into the string.
# Install released version from CRANinstall.packages("glue")# Install development version from GitHubpak::pak("tidyverse/glue")glue() makes it easy to interpolate data intostrings:
library(glue)name<-"Fred"glue("My name is {name}.")#> My name is Fred.stringr::str_glue() is an alias forglue::glue(). So if you’ve already attached stringr (orperhaps the whole tidyverse), you can usestr_glue() toaccess all of the functionality ofglue():
library(stringr)# or library(tidyverse)name<-"Wilma"str_glue("My name is {name}.")#> My name is Wilma.You’re not limited to using a bare symbol inside{}; itcan be any little bit of R code:
name<-"Pebbles"glue("Here is my name in uppercase and doubled: {strrep(toupper(name), 2)}.")#> Here is my name in uppercase and doubled: PEBBLESPEBBLES.glue can interpolate values from the local environment or from datapassed inname = value form:
x<-"the local environment"glue("`glue()` can access values from {x} or from {y}. {z}",y ="named arguments",z ="Woo!")#> `glue()` can access values from the local environment or from named arguments. Woo!If the relevant data lives in a data frame (or list or environment),useglue_data() instead:
mini_mtcars<-head(cbind(model =rownames(mtcars), mtcars))glue_data(mini_mtcars,"{model} has {hp} hp.")#> Mazda RX4 has 110 hp.#> Mazda RX4 Wag has 110 hp.#> Datsun 710 has 93 hp.#> Hornet 4 Drive has 110 hp.#> Hornet Sportabout has 175 hp.#> Valiant has 105 hp.glue_data() is very natural to use with the pipe:
mini_mtcars|>glue_data("{model} gets {mpg} miles per gallon.")#> Mazda RX4 gets 21 miles per gallon.#> Mazda RX4 Wag gets 21 miles per gallon.#> Datsun 710 gets 22.8 miles per gallon.#> Hornet 4 Drive gets 21.4 miles per gallon.#> Hornet Sportabout gets 18.7 miles per gallon.#> Valiant gets 18.1 miles per gallon.Theseglue_data() examples also demonstrate thatglue() is vectorized over the data.
glue() lets you write code that makes it easy to predictwhat the final string will look like. There is considerably lesssyntactical noise and mystery compared topaste() andsprintf().
Empty first and last lines are automatically trimmed, as is leadingwhitespace that is common across all lines. You don’t have to choosebetween indenting your code properly and getting the output you actuallywant. Consider what happens whenglue() is used inside thebody of a function:
foo<-function() {glue(" A formatted string Can have multiple lines with additional indention preserved")}foo()#> A formatted string#> Can have multiple lines#> with additional indention preservedThe leading whitespace that is common to all 3 lines is absent fromthe result.
glue is a relatively small and focused package, but there’s more toit than the basic usage ofglue() andglue_data() shown here. More recommended functions andresources:
vignette("glue"))demonstrates more interesting features ofglue() andglue_data().glue_sql() andglue_data_sql() arespecialized functions for producing SQL statements.vignette("engines", package = "glue").Please note that this project is released with aContributor Codeof Conduct. By participating in this project, you agree to abide byits terms.