glue v1.1.1

0

Monthly downloads

0th

Percentile

by Jim Hester

Interpreted String Literals

An implementation of interpreted string literals, inspired by Python's Literal String Interpolation <https://www.python.org/dev/peps/pep-0498/> and Docstrings <https://www.python.org/dev/peps/pep-0257/> and Julia's Triple-Quoted String Literals <https://docs.julialang.org/en/stable/manual/strings/#triple-quoted-string-literals>.

Readme

glue

CRAN\_Status\_Badge Travis-CI Build Status Coverage Status AppVeyor Build Status

Glue strings to data in R. Small, fast, dependency free interpreted string literals.

Installation

# install.packages("devtools")
devtools::install_github("tidyverse/glue")

Usage

Long strings are broken by line and concatenated together.
name <- "Fred"
age <- 50
anniversary <- 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.
Named arguments are 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_data() is useful with magrittr pipes.
`%>%` <- magrittr::`%>%`
head(mtcars) %>% glue_data("{rownames(.)} has {hp} hp")
#> Mazda RX4 has 110 hp
#> Mazda RX4 Wag has 110 hp
#> Datsun 710 has 93 hp
#> Hornet 4 Drive has 110 hp
#> Hornet Sportabout has 175 hp
#> Valiant has 105 hp
Leading whitespace and blank lines from the first and last lines are automatically trimmed.

This lets you indent the strings naturally in code.

glue("
    A formatted string
    Can have multiple lines
      with additional indention preserved
    ")
#> A formatted string
#> Can have multiple lines
#>   with additional indention preserved
An additional newline can be used if you want a leading or trailing newline.
glue("

  leading or trailing newlines can be added explicitly

  ")
#> 
#> leading or trailing newlines can be added explicitly
\\ at the end of a line continues it without a new line.
glue("
    A formatted string \\
    can also be on a \\
    single line
    ")
#> A formatted string can also be on a single line
A literal brace is inserted by using doubled braces.
name <- "Fred"
glue("My name is {name}, not {{name}}.")
#> My name is Fred, not {name}.
Alternative delimiters can be specified with .open and .close.
one <- "1"
glue("The value of $e^{2\\pi i}$ is $<<one>>$.", .open = "<<", .close = ">>")
#> The value of $e^{2\pi i}$ is $1$.
All valid R code works in expressions, including braces and escaping.

Backslashes do need to be doubled just like in all R strings.

  `foo}\`` <- "foo"
glue("{
      {
        '}\\'' # { and } in comments, single quotes
        \"}\\\"\" # or double quotes are ignored
        `foo}\\`` # as are { in backticks
      }
  }")
#> foo

Other implementations

Some other implementations of string interpolation in R (although not using identical syntax).

Functions in glue

Name Description
as_glue Coerce object to glue
collapse Collapse a character vector
glue Format and interpolate a string
quoting Quoting operators
trim Trim a character vector
No Results!

Last month downloads

Details

License MIT + file LICENSE
Encoding UTF-8
LazyData true
RoxygenNote 6.0.1
URL https://github.com/tidyverse/glue
BugReports https://github.com/tidyverse/glue/issues
NeedsCompilation yes
Packaged 2017-06-16 20:43:57 UTC; jhester
Repository CRAN
Date/Publication 2017-06-21 16:58:04 UTC
suggests covr , magrittr , testthat
imports methods
depends R (>= 3.0.0)
Contributors

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/glue)](http://www.rdocumentation.org/packages/glue)