Title: | Interpreted String Literals |
---|---|
Description: | An implementation of interpreted string literals. Based on the 'glue' package by Hester & Bryan (2024) <doi:10.32614/CRAN.package.glue> but with a focus on efficiency and simplicity at a cost of flexibility. |
Authors: | Tim Taylor [aut, cre] , Jim Hester [aut] , Jennifer Bryan [aut] , Posit Software, PBC [cph, fnd] |
Maintainer: | Tim Taylor <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.3 |
Built: | 2024-11-25 16:40:48 UTC |
Source: | CRAN |
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())
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}")) # 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}. ")) # single braces can be inserted by doubling them glue("My name is {name}, 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)
name <- "Fred" age <- 50 cat(glue("My name is {name} and my age next year is {age}")) # 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}. ")) # single braces can be inserted by doubling them glue("My name is {name}, 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)
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)
trim(x)
x |
|
A character vector.
cat(trim(" A formatted string Can have multiple lines with additional indentation preserved ")) cat(trim(" \ntrailing or leading newlines can be added explicitly\n ")) cat(trim(" A formatted string \\ can also be on a \\ single line "))
cat(trim(" A formatted string Can have multiple lines with additional indentation preserved ")) cat(trim(" \ntrailing or leading newlines can be added explicitly\n ")) cat(trim(" A formatted string \\ can also be on a \\ single line "))