str_glue: Format and interpolate a string with glue

Description Usage Arguments Examples

Description

These functions are wrappers around glue::glue() and glue::glue_data(), which provide a powerful and elegant syntax for interpolating strings. These wrappers provide a small set of the full options. Use the functions directly from glue for more control.

Usage

1
2
3
4
str_glue(..., .sep = "", .envir = parent.frame())

str_glue_data(.x, ..., .sep = "", .envir = parent.frame(),
  .na = "NA")

Arguments

...

[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.

.x

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

.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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
name <- "Fred"
age <- 50
anniversary <- as.Date("1991-10-12")
str_glue(
  "My name is {name}, ",
  "my age next year is {age + 1}, ",
  "and my anniversary is {format(anniversary, '%A, %B %d, %Y')}."
)

# single braces can be inserted by doubling them
str_glue("My name is {name}, not {{name}}.")

# You can also used named arguments
str_glue(
  "My name is {name}, ",
  "and my age next year is {age + 1}.",
  name = "Joe",
  age = 40
)

# `str_glue_data()` is useful in data pipelines
mtcars %>% str_glue_data("{rownames(.)} has {hp} hp")

Example output

My name is Fred, my age next year is 51, and my anniversary is Saturday, October 12, 1991.
My name is Fred, not {name}.
My name is Joe, and my age next year is 41.
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

stringr documentation built on May 2, 2019, 11:03 a.m.