Skip to content

Commit 1098b9e

Browse files
committed
structure paragraphs
1 parent 73b4119 commit 1098b9e

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

vignettes/ggplot2-in-packages.Rmd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ There are three situations in which you will encounter this problem:
7979
- You have the column name as a character vector.
8080
- The user specifies the column name or expression, and you want your function to use the same kind of non-standard evaluation used by `aes()` and `vars()`.
8181

82+
### Mapping is known in advance
83+
8284
If you already know the mapping in advance (like the above example) you should use the `.data` pronoun from [rlang](https://rlang.r-lib.org/) to make it explicit that you are referring to the `drv` in the layer data and not some other variable named `drv` (which may or may not exist elsewhere). To avoid a similar note from the CMD check about `.data`, use `#' @importFrom rlang .data` in any roxygen code block (typically this should be in the package documentation as generated by `usethis::use_package_doc()`).
8385

8486
```{r}
@@ -89,6 +91,8 @@ mpg_drv_summary <- function() {
8991
}
9092
```
9193

94+
### Character columns
95+
9296
If you have the column name as a character vector (e.g., `col = "drv"`), use `.data[[col]]`:
9397

9498
```{r}
@@ -101,6 +105,8 @@ col_summary <- function(df, col, by) {
101105
col_summary(mpg, "drv", "year")
102106
```
103107

108+
### Non-standard evaluation
109+
104110
If the column name or expression is supplied by the user, you can also pass it to `aes()` or `vars()` using `{{ col }}`. This tidy eval operator captures the expression supplied by the user and forwards it to another tidy eval-enabled function such as `aes()` or `vars()`.
105111

106112
```{r}
@@ -114,6 +120,8 @@ col_summary <- function(df, col, by) {
114120
col_summary(mpg, drv, year)
115121
```
116122

123+
### Summary
124+
117125
To summarise:
118126

119127
- If you know the mapping or facet specification is `col` in advance, use `aes(.data$col)` or `vars(.data$col)`.

0 commit comments

Comments
 (0)