Expressions enclosed by braces will be evaluated as R code. Long strings are broken by line and concatenated together. Leading whitespace and blank lines from the first and last lines are automatically trimmed.
glue_data( .x, ..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}", .na = "NA", .null = character(), .comment = "#", .transformer = identity_transformer, .trim = TRUE ) glue( ..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}", .na = "NA", .null = character(), .comment = "#", .transformer = identity_transformer, .trim = TRUE )
.x | [ |
---|---|
... | [ |
.sep | [ |
.envir | [ |
.open | [ |
.close | [ |
.na | [ |
.null | [ |
.comment | [ |
.transformer | [ |
.trim | [ |
https://www.python.org/dev/peps/pep-0498/ and https://www.python.org/dev/peps/pep-0257/ upon which this is based.
name <- "Fred" age <- 50 anniversary <- as.Date("1991-10-12") glue('My name is {name},', 'my age next year is {age + 1},', 'my anniversary is {format(anniversary, "%A, %B %d, %Y")}.') #> My name is Fred,my age next year is 51,my anniversary is Saturday, October 12, 1991. # single braces can be inserted by doubling them glue("My name is {name}, not {{name}}.") #> My name is Fred, not {name}. # Named arguments can be used to assign temporary variables. 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()` can also be used in user defined functions intro <- function(name, profession, country){ glue("My name is {name}, a {profession}, from {country}") } intro("Shelmith", "Senior Data Analyst", "Kenya") #> My name is Shelmith, a Senior Data Analyst, from Kenya intro("Cate", "Data Scientist", "Kenya") #> My name is Cate, a Data Scientist, from Kenya # `glue_data()` is useful in magrittr pipes if (require(magrittr)) { mtcars %>% glue_data("{rownames(.)} has {hp} hp") # Or within dplyr pipelines if (require(dplyr)) { head(iris) %>% mutate(description = glue("This {Species} has a petal length of {Petal.Length}")) }} #> Loading required package: magrittr #> Loading required package: dplyr #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union #> 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 # Alternative delimiters can also be used if needed one <- "1" glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>") #> The value of $e^{2\pi i}$ is $1$.