Glue strings to data in R. Small, fast, dependency free interpreted string literals.
Glue offers interpreted string literals that are small, fast, and dependency-free. Glue does this by embedding R expressions in curly braces which are then evaluated and inserted into the argument string.
# Install the released version from CRAN: install.packages("glue")Install the development version from GitHub:
install.packages("devtools")
devtools::install_github("tidyverse/glue")
library(glue) name My name is Fred.
library(glue)name My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.
glue('My name is {name},', ' my age next year is {age + 1},', ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.', name = "Joe", age = 40, anniversary = as.Date("2001-10-12")) #> My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.
glue_data()is useful with magrittr pipes.
`%>%` %` head(mtcars) %>% glue_data("{rownames(.)} 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
library(dplyr) head(iris) %>% mutate(description = glue("This {Species} has a petal length of {Petal.Length}")) #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> description #> 1 This setosa has a petal length of 1.4 #> 2 This setosa has a petal length of 1.4 #> 3 This setosa has a petal length of 1.3 #> 4 This setosa has a petal length of 1.5 #> 5 This setosa has a petal length of 1.4 #> 6 This setosa has a petal length of 1.7
This lets you indent the strings naturally in code.
glue(" A formatted string Can have multiple lines with additional indention preserved ") #> A formatted string #> Can have multiple lines #> with additional indention preserved
glue("leading or trailing newlines can be added explicitly
") #> #> leading or trailing newlines can be added explicitly
\\at the end of a line continues it without a new line.
glue(" A formatted string \\ can also be on a \\ single line ") #> A formatted string can also be on a single line
name My name is Fred, not {name}.
.openand
.close.
one >$.", .open = "<>") #> The value of $e^{2\pi i}$ is $1$.
Backslashes do need to be doubled just like in all R strings.
`foo}\`` foo
glue_sql()makes constructing SQL statements safe and easy
Use backticks to quote identifiers, normal strings and numbers are quoted appropriately for your backend.
library(glue)con {num} AND {
tbl
}.species = {val} ", .con = con) #> SELECTsepal_width
#> FROMiris
#> WHEREiris
.sepal_length > 2 #> ANDiris
.species = 'setosa'
glue_sql()
can be used in conjunction with parameterized queries using
DBI::dbBind()
to provide protection for SQL Injection attackssql ? ", .con = con) query sepal_width #> 1 3.5 #> 2 3.0 #> 3 3.2 #> 4 3.1 DBI::dbClearResult(query)
glue_sql()
can be used to build up more complex queries withinterchangeable sub queries. It returns
DBI::SQL()
objects which areproperly protected from quoting.
sub_query SELECT s.
sepal_width
#> FROM (SELECT * #> FROMiris
) AS sIf you want to input multiple values for use in SQL IN statements put
*
at the end of the value and the values will be collapsed and quoted appropriately.
glue_sql("SELECT * FROM {
tbl
} WHERE sepal_length IN ({vals*})", vals = 1, .con = con) #> SELECT * FROMiris
WHERE sepal_length IN (1)glue_sql("SELECT * FROM {
tbl
} WHERE sepal_length IN ({vals*})", vals = 1:5, .con = con) #> SELECT * FROMiris
WHERE sepal_length IN (1, 2, 3, 4, 5)glue_sql("SELECT * FROM {
tbl
} WHERE species IN ({vals*})", vals = "setosa", .con = con) #> SELECT * FROMiris
WHERE species IN ('setosa')glue_sql("SELECT * FROM {
tbl
} WHERE species IN ({vals*})", vals = c("setosa", "versicolor"), .con = con) #> SELECT * FROMiris
WHERE species IN ('setosa', 'versicolor')
+
x x + y = 4
Some other implementations of string interpolation in R (although not using identical syntax).
String templating is closely related to string interpolation, although not exactly the same concept. Some packages implementing string templating in R include.
Please note that the glue project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.