Skip to content

Commit e888c42

Browse files
authored
Quality of life improvements to select.R docs (#6245)
* Inline `setup.Rmd` so we can teardown afterwards to restore old options In particular, this restores `crayon.enabled` to its original state. Without this reset, most things in the tidyverse will print without color after running a check in the console. * Fix the width when running `select.R` examples This fixes the issue where running `devtools::check()` in the console would change the `select.Rd` file because the tibble output depended on the current width of the RStudio console * Use an explicit call to `knit_child()` wrapped in `with_options()`
1 parent f1f7195 commit e888c42

File tree

3 files changed

+84
-86
lines changed

3 files changed

+84
-86
lines changed

R/select.R

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,76 +38,18 @@
3838
#' \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("select")}.
3939
#'
4040
#' @section Examples:
41-
#' ```{r, child = "man/rmd/setup.Rmd"}
42-
#' ```
43-
#'
44-
#' Here we show the usage for the basic selection operators. See the
45-
#' specific help pages to learn about helpers like [starts_with()].
46-
#'
47-
#' The selection language can be used in functions like
48-
#' `dplyr::select()` or `tidyr::pivot_longer()`. Let's first attach
49-
#' the tidyverse:
50-
#'
51-
#' ```{r, comment = "#>", collapse = TRUE}
52-
#' library(tidyverse)
53-
#'
54-
#' # For better printing
55-
#' iris <- as_tibble(iris)
56-
#' ```
57-
#'
58-
#' Select variables by name:
59-
#'
60-
#' ```{r, comment = "#>", collapse = TRUE}
61-
#' starwars %>% select(height)
62-
#'
63-
#' iris %>% pivot_longer(Sepal.Length)
64-
#' ```
65-
#'
66-
#' Select multiple variables by separating them with commas. Note how
67-
#' the order of columns is determined by the order of inputs:
68-
#'
69-
#' ```{r, comment = "#>", collapse = TRUE}
70-
#' starwars %>% select(homeworld, height, mass)
71-
#' ```
72-
#'
73-
#' Functions like `tidyr::pivot_longer()` don't take variables with
74-
#' dots. In this case use `c()` to select multiple variables:
75-
#'
76-
#' ```{r, comment = "#>", collapse = TRUE}
77-
#' iris %>% pivot_longer(c(Sepal.Length, Petal.Length))
78-
#' ```
79-
#'
80-
#' ## Operators:
81-
#'
82-
#' The `:` operator selects a range of consecutive variables:
83-
#'
84-
#' ```{r, comment = "#>", collapse = TRUE}
85-
#' starwars %>% select(name:mass)
86-
#' ```
87-
#'
88-
#' The `!` operator negates a selection:
89-
#'
90-
#' ```{r, comment = "#>", collapse = TRUE}
91-
#' starwars %>% select(!(name:mass))
92-
#'
93-
#' iris %>% select(!c(Sepal.Length, Petal.Length))
94-
#'
95-
#' iris %>% select(!ends_with("Width"))
96-
#' ```
97-
#'
98-
#' `&` and `|` take the intersection or the union of two selections:
99-
#'
100-
#' ```{r, comment = "#>", collapse = TRUE}
101-
#' iris %>% select(starts_with("Petal") & ends_with("Width"))
102-
#'
103-
#' iris %>% select(starts_with("Petal") | ends_with("Width"))
104-
#' ```
105-
#'
106-
#' To take the difference between two selections, combine the `&` and
107-
#' `!` operators:
10841
#'
109-
#' ```{r, comment = "#>", collapse = TRUE}
110-
#' iris %>% select(starts_with("Petal") & !ends_with("Width"))
42+
#' ```{r, echo = FALSE, results = "asis"}
43+
#' result <- rlang::with_options(
44+
#' knitr::knit_child("man/rmd/select.Rmd"),
45+
#' tibble.print_min = 4,
46+
#' tibble.max_extra_cols = 8,
47+
#' digits = 2,
48+
#' crayon.enabled = FALSE,
49+
#' cli.unicode = FALSE,
50+
#' width = 80
51+
#' )
52+
#' cat(result, sep = "\n")
11153
#' ```
11254
#'
11355
#' @family single table verbs

man/rmd/select.Rmd

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
```{r, include=FALSE}
2+
# So the second library() call doesn't show messages
3+
library(tidyverse)
4+
```
5+
6+
Here we show the usage for the basic selection operators. See the
7+
specific help pages to learn about helpers like [starts_with()].
8+
9+
The selection language can be used in functions like
10+
`dplyr::select()` or `tidyr::pivot_longer()`. Let's first attach
11+
the tidyverse:
12+
13+
```{r, comment = "#>", collapse = TRUE}
14+
library(tidyverse)
15+
16+
# For better printing
17+
iris <- as_tibble(iris)
18+
```
19+
20+
Select variables by name:
21+
22+
```{r, comment = "#>", collapse = TRUE}
23+
starwars %>% select(height)
24+
25+
iris %>% pivot_longer(Sepal.Length)
26+
```
27+
28+
Select multiple variables by separating them with commas. Note how
29+
the order of columns is determined by the order of inputs:
30+
31+
```{r, comment = "#>", collapse = TRUE}
32+
starwars %>% select(homeworld, height, mass)
33+
```
34+
35+
Functions like `tidyr::pivot_longer()` don't take variables with
36+
dots. In this case use `c()` to select multiple variables:
37+
38+
```{r, comment = "#>", collapse = TRUE}
39+
iris %>% pivot_longer(c(Sepal.Length, Petal.Length))
40+
```
41+
42+
## Operators:
43+
44+
The `:` operator selects a range of consecutive variables:
45+
46+
```{r, comment = "#>", collapse = TRUE}
47+
starwars %>% select(name:mass)
48+
```
49+
50+
The `!` operator negates a selection:
51+
52+
```{r, comment = "#>", collapse = TRUE}
53+
starwars %>% select(!(name:mass))
54+
55+
iris %>% select(!c(Sepal.Length, Petal.Length))
56+
57+
iris %>% select(!ends_with("Width"))
58+
```
59+
60+
`&` and `|` take the intersection or the union of two selections:
61+
62+
```{r, comment = "#>", collapse = TRUE}
63+
iris %>% select(starts_with("Petal") & ends_with("Width"))
64+
65+
iris %>% select(starts_with("Petal") | ends_with("Width"))
66+
```
67+
68+
To take the difference between two selections, combine the `&` and
69+
`!` operators:
70+
71+
```{r, comment = "#>", collapse = TRUE}
72+
iris %>% select(starts_with("Petal") & !ends_with("Width"))
73+
```

man/rmd/setup.Rmd

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)