-- T --
trim()
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.
glue(x, env = parent.frame())
glut(x, env = parent.frame())
x |
|
env |
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 |
A character
object.
glue::glue_safe()
and glue::glue_data_safe()
on which which this
function is an evolution.
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."
Almost identical to glue::trim()
save a slight difference in error
handling for non-character input. This function trims a character
vector according to the trimming rules used by glue. These follow similar
rules to Python Docstrings,
with the following features:
Leading and trailing whitespace from the first and last lines is removed.
A uniform amount of indentation is stripped from the second line on, equal to the minimum indentation of all non-blank lines after the first.
Lines can be continued across newlines by using \\
.
trim(x)
x |
|
A character vector.
cat(trim("
A formatted string
Can have multiple lines
with additional indentation preserved
"))
#> A formatted string
#> Can have multiple lines
#> with additional indentation preserved
cat(trim("
\ntrailing or leading newlines can be added explicitly\n
"))
#> trailing or leading newlines can be added explicitly
cat(trim("
A formatted string \\
can also be on a \\
single line
"))
#> A formatted string can also be on a single line