glue: Format and interpolate a string

Description Usage Arguments See Also Examples

Description

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.

Usage

1
2
3
4
5
6
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)

Arguments

.x

[listish]
An environment, list or data frame used to lookup values.

...

[expressions]
Expressions string(s) to format, multiple inputs are concatenated together before formatting.

.sep

[character(1): ‘""’]
Separator used to separate elements.

.envir

[environment: parent.frame()]
Environment to evaluate each expression in. Expressions are evaluated from left to right. If .x is an environment, the expressions are evaluated in that environment and .envir is ignored.

.open

[character(1): ‘{’]
The opening delimiter. Doubling the full delimiter escapes it.

.close

[character(1): ‘}’]
The closing delimiter. Doubling the full delimiter escapes it.

.na

[character(1): ‘NA’]
Value to replace NA values with. If NULL missing values are propagated, that is an NA result will cause NA output. Otherwise the value is replaced by the value of .na.

.transformer

[function]
A function taking three parameters code, envir and data used to transform the output of each block before during or after evaluation. For example transformers see vignette("transformers").

.trim

[logical(1): ‘TRUE’]
Whether to trim the input template with trim() or not.

See Also

https://www.python.org/dev/peps/pep-0498/ and https://www.python.org/dev/peps/pep-0257 upon which this is based.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 = ">>")

Example output

My name is Fred,my age next year is 51,my anniversary is Saturday, October 12, 1991.
My name is Fred, not {name}.
My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.
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
Duster 360 has 245 hp
Merc 240D has 62 hp
Merc 230 has 95 hp
Merc 280 has 123 hp
Merc 280C has 123 hp
Merc 450SE has 180 hp
Merc 450SL has 180 hp
Merc 450SLC has 180 hp
Cadillac Fleetwood has 205 hp
Lincoln Continental has 215 hp
Chrysler Imperial has 230 hp
Fiat 128 has 66 hp
Honda Civic has 52 hp
Toyota Corolla has 65 hp
Toyota Corona has 97 hp
Dodge Challenger has 150 hp
AMC Javelin has 150 hp
Camaro Z28 has 245 hp
Pontiac Firebird has 175 hp
Fiat X1-9 has 66 hp
Porsche 914-2 has 91 hp
Lotus Europa has 113 hp
Ford Pantera L has 264 hp
Ferrari Dino has 175 hp
Maserati Bora has 335 hp
Volvo 142E has 109 hp

Attaching package: 'dplyr'

The following object is masked from 'package:glue':

    collapse

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
The value of $e^{2\pi i}$ is $1$.

glue documentation built on May 1, 2019, 9:45 p.m.