Learn R Programming

super (version 0.1.1)

glue: Format and interpolate a string

Description

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())

Value

A character object.

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

Run this code
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)

Run the code above in your browser using DataLab