You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Work on README
* Work on "Get started" vignette
* Was in the wrong place
* Apply suggestions from code review
Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
* Use function body example here too; rebuild README.md
* Update vignettes/glue.Rmd
Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
* Take the chance to talk about the glue class and printing
* Add as.character()
---------
Co-authored-by: Hadley Wickham <h.wickham@gmail.com>
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 argument string.
24
+
glue offers interpreted string literals that are small, fast, and dependency-free.
25
+
glue does this by embedding R expressions in curly braces, which are then evaluated and inserted into the string.
27
26
28
27
## Installation
29
28
@@ -49,136 +48,87 @@ pak::pak("tidyverse/glue")
49
48
library(glue)
50
49
51
50
name <- "Fred"
52
-
glue('My name is {name}.')
53
-
54
-
# A literal brace is inserted by using doubled braces.
55
-
name <- "Fred"
56
-
glue("My name is {name}, not {{name}}.")
51
+
glue("My name is {name}.")
57
52
```
58
53
59
-
`glue::glue()` is also made available via `stringr::str_glue()`.
60
-
So if you've already attached stringr (or perhaps the whole tidyverse), you can access `glue()`like so:
54
+
`stringr::str_glue()` is an alias for `glue::glue()`.
55
+
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()`:
61
56
62
57
```{r eval = FALSE}
63
58
library(stringr) # or library(tidyverse)
64
59
65
-
stringr_fcn <- "`stringr::str_glue()`"
66
-
glue_fcn <- "`glue::glue()`"
67
-
68
-
str_glue('{stringr_fcn} is essentially an alias for {glue_fcn}.')
69
-
#> `stringr::str_glue()` is essentially an alias for `glue::glue()`.
60
+
name <- "Wilma"
61
+
str_glue("My name is {name}.")
62
+
#> My name is Wilma.
70
63
```
71
64
72
-
`glue_data()` works well with pipes:
65
+
You're not limited to using a bare symbol inside `{}`; it can be any little bit of R code:
73
66
74
67
```{r}
75
-
mtcars$model <- rownames(mtcars)
76
-
mtcars |> head() |> glue_data("{model} has {hp} hp")
68
+
name <- "Pebbles"
69
+
glue("Here is my name in uppercase and doubled: {strrep(toupper(name), 2)}.")
77
70
```
78
71
79
-
##### `glue_data()` is useful with [magrittr](https://cran.r-project.org/package=magrittr) pipes.
80
-
```{r}
81
-
`%>%` <- magrittr::`%>%`
82
-
head(mtcars) %>% glue_data("{rownames(.)} has {hp} hp")
83
-
```
72
+
### Data can come from various sources
84
73
85
-
##### `glue()` is useful within dplyr pipelines
86
-
```{r, message = FALSE}
87
-
library(dplyr)
88
-
head(iris) %>%
89
-
mutate(description = glue("This {Species} has a petal length of {Petal.Length}"))
90
-
```
74
+
glue can interpolate values from the local environment or from data passed in `name = value` form:
91
75
92
-
##### Leading whitespace and blank lines from the first and last lines are automatically trimmed.
93
-
This lets you indent the strings naturally in code.
94
76
```{r}
95
-
glue("
96
-
A formatted string
97
-
Can have multiple lines
98
-
with additional indention preserved
99
-
")
77
+
x <- "the local environment"
78
+
glue(
79
+
"`glue()` can access values from {x} or from {y}. {z}",
80
+
y = "named arguments",
81
+
z = "Woo!"
82
+
)
100
83
```
101
84
102
-
##### An additional newline can be used if you want a leading or trailing newline.
103
-
```{r}
104
-
glue("
105
-
106
-
leading or trailing newlines can be added explicitly
85
+
If the relevant data lives in a data frame (or list or environment), use `glue_data()` instead:
107
86
108
-
")
109
-
```
110
-
111
-
##### `\\` at the end of a line continues it without a new line.
* The "Get started" article (`vignette("glue")`) demonstrates more interesting features of `glue()` and `glue_data()`.
128
+
*`glue_sql()` and `glue_data_sql()` are specialized functions for producing SQL statements.
129
+
* glue provides a couple of custom knitr engines that allow you to use glue syntax in chunks; learn more in `vignette("engines", package = "glue")`.
181
130
182
131
## Code of Conduct
183
132
184
-
Please note that the glue project is released with a [Contributor Code of Conduct](https://glue.tidyverse.org/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.
133
+
Please note that this project is released with a [Contributor Code of Conduct](https://glue.tidyverse.org/CODE_OF_CONDUCT.html).
134
+
By participating in this project, you agree to abide by its terms.
0 commit comments