Inputs enclosed by braces (e.g. {name}
) are looked up in the provided
environment (akin to calling get()
). Single braces can be escaped by
doubling them up. Variables are recycled to the length of the largest one.
glue()
operates on the string as is.
glut()
will trim
the input prior to glueing.
Usage
glue(x, env = parent.frame())
glut(x, env = parent.frame())
Arguments
- x
[character string]
- env
[environment]
Where to look up the embraced input.
Can be an environment or a list-like object that will be converted in the underlying function via
list2env()
.
See also
glue::glue_safe()
and glue::glue_data_safe()
on which which this
function is an evolution.
Examples
name <- "Fred"
age <- 50
cat(glue("My name is {name} and my age next year is {age}"))
#> My name is Fred and my age next year is 50
# glut first trims the output
anniversary <- as.Date("1991-10-12")
cat(glut("
My name is {name},
my age next year is {age},
my anniversary is {anniversary}.
"))
#> My name is Fred,
#> my age next year is 50,
#> my anniversary is 1991-10-12.
# single braces can be inserted by doubling them
glue("My name is {name}, not {{name}}.")
#> [1] "My name is Fred, not {name}."
# List like objects can be used in place of an environment
dat <- cbind(car = rownames(mtcars), mtcars)
glue("{car} does {mpg} mpg.", dat)
#> [1] "Mazda RX4 does 21 mpg." "Mazda RX4 Wag does 21 mpg."
#> [3] "Datsun 710 does 22.8 mpg." "Hornet 4 Drive does 21.4 mpg."
#> [5] "Hornet Sportabout does 18.7 mpg." "Valiant does 18.1 mpg."
#> [7] "Duster 360 does 14.3 mpg." "Merc 240D does 24.4 mpg."
#> [9] "Merc 230 does 22.8 mpg." "Merc 280 does 19.2 mpg."
#> [11] "Merc 280C does 17.8 mpg." "Merc 450SE does 16.4 mpg."
#> [13] "Merc 450SL does 17.3 mpg." "Merc 450SLC does 15.2 mpg."
#> [15] "Cadillac Fleetwood does 10.4 mpg." "Lincoln Continental does 10.4 mpg."
#> [17] "Chrysler Imperial does 14.7 mpg." "Fiat 128 does 32.4 mpg."
#> [19] "Honda Civic does 30.4 mpg." "Toyota Corolla does 33.9 mpg."
#> [21] "Toyota Corona does 21.5 mpg." "Dodge Challenger does 15.5 mpg."
#> [23] "AMC Javelin does 15.2 mpg." "Camaro Z28 does 13.3 mpg."
#> [25] "Pontiac Firebird does 19.2 mpg." "Fiat X1-9 does 27.3 mpg."
#> [27] "Porsche 914-2 does 26 mpg." "Lotus Europa does 30.4 mpg."
#> [29] "Ford Pantera L does 15.8 mpg." "Ferrari Dino does 19.7 mpg."
#> [31] "Maserati Bora does 15 mpg." "Volvo 142E does 21.4 mpg."