| glue {glue} | R Documentation |
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", .transformer = identity_transformer,
.trim = TRUE)
glue(..., .sep = "", .envir = parent.frame(), .open = "{",
.close = "}", .na = "NA", .transformer = identity_transformer)
.x |
[ |
... |
[ |
.sep |
[ |
.envir |
[ |
.open |
[ |
.close |
[ |
.na |
[ |
.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")}.')
# single braces can be inserted by doubling them
glue("My name is {name}, 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"))
# `glue_data()` is useful in magrittr pipes
library(magrittr)
mtcars %>% glue_data("{rownames(.)} has {hp} hp")
# Or within dplyr pipelines
library(dplyr)
head(iris) %>%
mutate(description = glue("This {Species} has a petal length of {Petal.Length}"))
# Alternative delimiters can also be used if needed
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")