Skip to content

Commit e2291d0

Browse files
authored
Make vector recycling rules its own FAQ (#1885)
* Make vector recycling rules its own FAQ * Add in extra new line * Use manual pkgdown redirects
1 parent c5a8276 commit e2291d0

17 files changed

+109
-108
lines changed

R/bind.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ vec_rbind <- fn_inline_formals(vec_rbind, ".name_repair")
187187
#' @rdname vec_bind
188188
#' @param .size If, `NULL`, the default, will determine the number of rows in
189189
#' `vec_cbind()` output by using the tidyverse [recycling
190-
#' rules][vector_recycling_rules].
190+
#' rules][theory-faq-recycling].
191191
#'
192192
#' Alternatively, specify the desired number of rows, and any inputs of length
193193
#' 1 will be recycled appropriately.

R/faq-developer.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ NULL
1212
#' @name theory-faq-coercion
1313
NULL
1414

15+
# Also see the `redirects:` section in `_pkgdown.yml`
16+
# for `vector_recycling_rules.html`
17+
18+
#' FAQ - How does recycling work in vctrs and the tidyverse?
19+
#'
20+
#' @includeRmd man/faq/developer/theory-recycling.Rmd description
21+
#'
22+
#' @name theory-faq-recycling
23+
#' @aliases vector_recycling_rules
24+
NULL
25+
1526
#' FAQ - How to implement ptype2 and cast methods?
1627
#'
1728
#' @includeRmd man/faq/developer/howto-coercion.Rmd description

R/recycle.R

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,8 @@
1-
#' Recycling rules used by r-lib and the tidyverse
2-
#'
3-
#' @description
4-
#' Recycling describes the concept of repeating elements of one vector to match
5-
#' the size of another. There are two rules that underlie the "tidyverse"
6-
#' recycling rules:
7-
#'
8-
#' - Vectors of size 1 will be recycled to the size of any other vector
9-
#'
10-
#' - Otherwise, all vectors must have the same size
11-
#'
12-
#' @section Examples:
13-
#'
14-
#' ```{r, warning = FALSE, message = FALSE, include = FALSE}
15-
#' library(tibble)
16-
#' ```
17-
#'
18-
#' Vectors of size 1 are recycled to the size of any other vector:
19-
#'
20-
#' ```{r, comment = "#>"}
21-
#' tibble(x = 1:3, y = 1L)
22-
#' ```
23-
#'
24-
#' This includes vectors of size 0:
25-
#'
26-
#' ```{r, comment = "#>"}
27-
#' tibble(x = integer(), y = 1L)
28-
#' ```
29-
#'
30-
#' If vectors aren't size 1, they must all be the same size. Otherwise, an error
31-
#' is thrown:
32-
#'
33-
#' ```{r, comment = "#>", error = TRUE}
34-
#' tibble(x = 1:3, y = 4:7)
35-
#' ```
36-
#'
37-
#' @section vctrs backend:
38-
#'
39-
#' Packages in r-lib and the tidyverse generally use [vec_size_common()] and
40-
#' [vec_recycle_common()] as the backends for handling recycling rules.
41-
#'
42-
#' - `vec_size_common()` returns the common size of multiple vectors, after
43-
#' applying the recycling rules
44-
#'
45-
#' - `vec_recycle_common()` goes one step further, and actually recycles the
46-
#' vectors to their common size
47-
#'
48-
#' ```{r, comment = "#>", error = TRUE}
49-
#' vec_size_common(1:3, "x")
50-
#'
51-
#' vec_recycle_common(1:3, "x")
52-
#'
53-
#' vec_size_common(1:3, c("x", "y"))
54-
#' ```
55-
#'
56-
#' @section Base R recycling rules:
57-
#'
58-
#' The recycling rules described here are stricter than the ones generally used
59-
#' by base R, which are:
60-
#'
61-
#' - If any vector is length 0, the output will be length 0
62-
#'
63-
#' - Otherwise, the output will be length `max(length_x, length_y)`, and a
64-
#' warning will be thrown if the length of the longer vector is not an integer
65-
#' multiple of the length of the shorter vector.
66-
#'
67-
#' We explore the base R rules in detail in `vignette("type-size")`.
68-
#'
69-
#' @name vector_recycling_rules
70-
#' @keywords internal
71-
NULL
72-
731
#' Vector recycling
742
#'
753
#' `vec_recycle(x, size)` recycles a single vector to a given size.
764
#' `vec_recycle_common(...)` recycles multiple vectors to their common size. All
77-
#' functions obey the [vctrs recycling rules][vector_recycling_rules], and will
5+
#' functions obey the [vctrs recycling rules][theory-faq-recycling], and will
786
#' throw an error if recycling is not possible. See [vec_size()] for the precise
797
#' definition of size.
808
#'

R/rep.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#' the entire vector.
3737
#'
3838
#' For `vec_rep_each()`, an integer vector of the number of times to repeat
39-
#' each element of `x`. `times` will be [recycled][vector_recycling_rules] to
39+
#' each element of `x`. `times` will be [recycled][theory-faq-recycling] to
4040
#' the size of `x`.
4141
#' @param x_arg,times_arg Argument names for errors.
4242
#'

R/size.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' to `vec_size()` as [lengths()] is to [length()].
2020
#'
2121
#' @seealso [vec_slice()] for a variation of `[` compatible with `vec_size()`,
22-
#' and [vec_recycle()] to [recycle][vector_recycling_rules] vectors to common
22+
#' and [vec_recycle()] to [recycle][theory-faq-recycling] vectors to common
2323
#' length.
2424
#' @section Invariants:
2525
#' * `vec_size(dataframe)` == `vec_size(dataframe[[i]])`

R/slice-interleave.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' @inheritParams vec_c
2121
#'
2222
#' @param ... Vectors to interleave. These will be
23-
#' [recycled][vector_recycling_rules] to a common size.
23+
#' [recycled][theory-faq-recycling] to a common size.
2424
#'
2525
#' @export
2626
#' @examples

R/type-data-frame.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ new_data_frame <- fn_inline_formals(new_data_frame, "x")
3939
#'
4040
#' @section Properties:
4141
#'
42-
#' - Inputs are [recycled][vector_recycling_rules] to a common size with
42+
#' - Inputs are [recycled][theory-faq-recycling] to a common size with
4343
#' [vec_recycle_common()].
4444
#'
4545
#' - With the exception of data frames, inputs are not modified in any way.

_pkgdown.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,10 @@ reference:
131131
- vec_names
132132
- vec_as_location
133133
- vec_as_subscript
134+
135+
# Needed to generate a `vector_recycling_rules` page, since it is an `@alias`
136+
# but pkgdown doesn't currently generate pages for aliases. Other packages
137+
# link to this old page name, so we don't want it to disappear.
138+
# https://github.com/r-lib/pkgdown/issues/1876
139+
redirects:
140+
- ["reference/vector_recycling_rules.html", "reference/theory-faq-recycling.html"]

man/data_frame.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/df_list.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)