-
Notifications
You must be signed in to change notification settings - Fork 65
/
README.Rmd
134 lines (99 loc) · 4.13 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
library(glue)
```
# glue <a href="https://glue.tidyverse.org"><img src="man/figures/logo.png" align="right" height="138" alt="glue website" /></a>
<!-- badges: start -->
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/glue)](https://cran.r-project.org/package=glue)
[![R-CMD-check](https://github.com/tidyverse/glue/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidyverse/glue/actions/workflows/R-CMD-check.yaml)
[![test-coverage](https://github.com/tidyverse/glue/actions/workflows/test-coverage.yaml/badge.svg)](https://github.com/tidyverse/glue/actions/workflows/test-coverage.yaml)
<!-- badges: end -->
glue offers interpreted string literals that are small, fast, and dependency-free.
glue does this by embedding R expressions in curly braces, which are then evaluated and inserted into the string.
## Installation
::: .pkgdown-release
```{r, eval = FALSE}
# Install released version from CRAN
install.packages("glue")
```
:::
::: .pkgdown-devel
```{r, eval = FALSE}
# Install development version from GitHub
pak::pak("tidyverse/glue")
```
:::
## Usage
`glue()` makes it easy to interpolate data into strings:
```{r}
library(glue)
name <- "Fred"
glue("My name is {name}.")
```
`stringr::str_glue()` is an alias for `glue::glue()`.
So if you've already attached stringr (or perhaps the whole tidyverse), you can use `str_glue()` to access all of the functionality of `glue()`:
```{r eval = FALSE}
library(stringr) # or library(tidyverse)
name <- "Wilma"
str_glue("My name is {name}.")
#> My name is Wilma.
```
You're not limited to using a bare symbol inside `{}`; it can be any little bit of R code:
```{r}
name <- "Pebbles"
glue("Here is my name in uppercase and doubled: {strrep(toupper(name), 2)}.")
```
### Data can come from various sources
glue can interpolate values from the local environment or from data passed in `name = value` form:
```{r}
x <- "the local environment"
glue(
"`glue()` can access values from {x} or from {y}. {z}",
y = "named arguments",
z = "Woo!"
)
```
If the relevant data lives in a data frame (or list or environment), use `glue_data()` instead:
```{r}
mini_mtcars <- head(cbind(model = rownames(mtcars), mtcars))
glue_data(mini_mtcars, "{model} has {hp} hp.")
```
`glue_data()` is very natural to use with the pipe:
```{r}
mini_mtcars |>
glue_data("{model} gets {mpg} miles per gallon.")
```
These `glue_data()` examples also demonstrate that `glue()` is vectorized over the data.
### What you see is awfully close to what you get
`glue()` lets you write code that makes it easy to predict what the final string will look like.
There is considerably less syntactical noise and mystery compared to `paste()` and `sprintf()`.
Empty first and last lines are automatically trimmed, as is leading whitespace that is common across all lines.
You don't have to choose between indenting your code properly and getting the output you actually want.
Consider what happens when `glue()` is used inside the body of a function:
```{r}
foo <- function() {
glue("
A formatted string
Can have multiple lines
with additional indentation preserved")
}
foo()
```
The leading whitespace that is common to all 3 lines is absent from the result.
## Learning more
glue is a relatively small and focused package, but there's more to it than the basic usage of `glue()` and `glue_data()` shown here.
More recommended functions and resources:
* The "Get started" article (`vignette("glue")`) demonstrates more interesting features of `glue()` and `glue_data()`.
* `glue_sql()` and `glue_data_sql()` are specialized functions for producing SQL statements.
* glue provides a couple of custom knitr engines that allow you to use glue syntax in chunks; learn more in `vignette("engines", package = "glue")`.
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](https://glue.tidyverse.org/CODE_OF_CONDUCT.html).
By participating in this project, you agree to abide by its terms.