Format and interpolate a string — glue (2024)

Table of Contents
Usage Arguments Value See also Examples

Format and interpolate a string — glue (1)

Source: R/glue.R

glue.Rd

Expressions enclosed by braces will be evaluated as R code. Long strings arebroken by line and concatenated together. Leading whitespace and blank linesfrom the first and last lines are automatically trimmed.

Usage

glue_data( .x, ..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}", .na = "NA", .null = character(), .comment = "#", .literal = FALSE, .transformer = identity_transformer, .trim = TRUE)glue( ..., .sep = "", .envir = parent.frame(), .open = "{", .close = "}", .na = "NA", .null = character(), .comment = "#", .literal = FALSE, .transformer = identity_transformer, .trim = TRUE)

Arguments

.x

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

...

[expressions]
Unnamed arguments are taken to be expressionstring(s) to format. Multiple inputs are concatenated together before formatting.Named arguments are taken to be temporary variables available for substitution.

For glue_data(), elements in ... override the values in .x.

.sep

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

.envir

[environment: parent.frame()]
Environment to evaluate each expression in. Expressions areevaluated from left to right. If .x is an environment, the expressions areevaluated in that environment and .envir is ignored. If NULL is passed, it is equivalent to emptyenv().

.open

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

.close

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

.na

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

.null

[character(1): ‘character()’]
Value to replaceNULL values with. If character() whole output is character(). IfNULL all NULL values are dropped (as in paste0()). Otherwise thevalue is replaced by the value of .null.

.comment

[character(1): ‘#’]
Value to use as the commentcharacter.

.literal

[boolean(1): ‘FALSE’]
Whether to treat single ordouble quotes, backticks, and comments as regular characters (vs. assyntactic elements), when parsing the expression string. Setting .literal = TRUE probably only makes sense in combination with a custom.transformer, as is the case with glue_col(). Regard this argument(especially, its name) as experimental.

.transformer

[function]
A function taking two arguments, textand envir, where text is the unparsed string inside the glue block andenvir is the execution environment. A .transformer lets you modify aglue block before, during, or after evaluation, allowing you to create yourown custom glue()-like functions. See vignette("transformers") forexamples.

.trim

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

Value

A glue object, as created by as_glue().

Examples

name <- "Fred"age <- 50anniversary <- 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")}.')#> My name is Fred,my age next year is 51,my anniversary is Saturday, October 12, 1991.# single braces can be inserted by doubling themglue("My name is {name}, not {{name}}.")#> My name is Fred, 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"))#> My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.# `glue()` can also be used in user defined functionsintro <- function(name, profession, country){ glue("My name is {name}, a {profession}, from {country}")}intro("Shelmith", "Senior Data Analyst", "Kenya")#> My name is Shelmith, a Senior Data Analyst, from Kenyaintro("Cate", "Data Scientist", "Kenya")#> My name is Cate, a Data Scientist, from Kenya# `glue_data()` is useful in magrittr pipesif (require(magrittr)) {mtcars %>% glue_data("{rownames(.)} has {hp} hp")# Or within dplyr pipelinesif (require(dplyr)) {head(iris) %>% mutate(description = glue("This {Species} has a petal length of {Petal.Length}"))}}#> Loading required package: magrittr#> Loading required package: dplyr#> #> Attaching package: ‘dplyr’#> 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# Alternative delimiters can also be used if neededone <- "1"glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")#> The value of $e^{2\pi i}$ is $1$.
Format and interpolate a string — glue (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Chrissy Homenick

Last Updated:

Views: 5762

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.