Skip to content

Commit c252a61

Browse files
Add .vary argument to expand_grid() (#1571)
* Add .vary parameter to expand_grid(), Fixes #1543 * Tweaks on documentation * Tweak NEWS bullet * Rework tests a little --------- Co-authored-by: Davis Vaughan <[email protected]>
1 parent 6707acc commit c252a61

File tree

6 files changed

+79
-46
lines changed

6 files changed

+79
-46
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# tidyr (development version)
22

3+
* `expand_grid()` gains a new `.vary` argument, allowing users to control
4+
whether the first column varies fastest or slowest (#1543, @JamesHWade).
5+
36
* `unite()` no longer errors if you provide a selection that doesn't select any
47
columns. Instead, it returns a column containing the empty string (#1548,
58
@catalamarti).

R/expand.R

+17-9
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,37 @@ nesting <- function(..., .name_repair = "check_unique") {
162162
#' `expand_grid()` is heavily motivated by [expand.grid()].
163163
#' Compared to `expand.grid()`, it:
164164
#'
165-
#' * Produces sorted output (by varying the first column the slowest, rather
166-
#' than the fastest).
165+
#' * Produces sorted output by varying the first column the slowest by default.
167166
#' * Returns a tibble, not a data frame.
168167
#' * Never converts strings to factors.
169168
#' * Does not add any additional attributes.
170169
#' * Can expand any generalised vector, including data frames.
171170
#'
171+
#' @inheritParams vctrs::vec_expand_grid
172+
#'
172173
#' @param ... Name-value pairs. The name will become the column name in the
173174
#' output.
174-
#' @inheritParams tibble::as_tibble
175-
#' @return A tibble with one column for each input in `...`. The output
176-
#' will have one row for each combination of the inputs, i.e. the size
177-
#' be equal to the product of the sizes of the inputs. This implies
178-
#' that if any input has length 0, the output will have zero rows.
175+
#'
176+
#' @return A tibble with one column for each input in `...`. The output will
177+
#' have one row for each combination of the inputs, i.e. the size will be
178+
#' equal to the product of the sizes of the inputs. This implies that if any
179+
#' input has length 0, the output will have zero rows. The ordering of the
180+
#' output depends on the `.vary` argument.
181+
#'
179182
#' @export
180183
#' @examples
184+
#' # Default behavior varies the first column "slowest"
181185
#' expand_grid(x = 1:3, y = 1:2)
182-
#' expand_grid(l1 = letters, l2 = LETTERS)
186+
#'
187+
#' # Vary the first column "fastest", like `expand.grid()`
188+
#' expand_grid(x = 1:3, y = 1:2, .vary = "fastest")
183189
#'
184190
#' # Can also expand data frames
185191
#' expand_grid(df = tibble(x = 1:2, y = c(2, 1)), z = 1:3)
192+
#'
186193
#' # And matrices
187194
#' expand_grid(x1 = matrix(1:4, nrow = 2), x2 = matrix(5:8, nrow = 2))
188-
expand_grid <- function(..., .name_repair = "check_unique") {
195+
expand_grid <- function(..., .name_repair = "check_unique", .vary = "slowest") {
189196
out <- grid_dots(...)
190197

191198
names <- names2(out)
@@ -202,6 +209,7 @@ expand_grid <- function(..., .name_repair = "check_unique") {
202209

203210
out <- vec_expand_grid(
204211
!!!out,
212+
.vary = .vary,
205213
.name_repair = "minimal",
206214
.error_call = current_env()
207215
)

man/expand.Rd

+3-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/expand_grid.Rd

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/expand.md

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
* `x` -> `x...1`
5656
* `x` -> `x...2`
5757

58+
# expand_grid() throws an error for invalid `.vary` parameter
59+
60+
Code
61+
expand_grid(x = 1:2, y = 1:2, .vary = "invalid")
62+
Condition
63+
Error in `expand_grid()`:
64+
! `.vary` must be one of "slowest" or "fastest", not "invalid".
65+
5866
# grid_dots() reject non-vector input
5967

6068
Code

tests/testthat/test-expand.R

+26
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,32 @@ test_that("expand_grid() works with 0 row tibbles", {
390390
expect_identical(expand_grid(df, x = 1:2), tibble(x = integer()))
391391
})
392392

393+
test_that("expand_grid() respects `.vary` parameter", {
394+
# Slowest
395+
expect_identical(
396+
expand_grid(x = 1:2, y = 1:2),
397+
tibble(
398+
x = c(1L, 1L, 2L, 2L),
399+
y = c(1L, 2L, 1L, 2L)
400+
)
401+
)
402+
403+
# Fastest
404+
expect_identical(
405+
expand_grid(x = 1:2, y = 1:2, .vary = "fastest"),
406+
tibble(
407+
x = c(1L, 2L, 1L, 2L),
408+
y = c(1L, 1L, 2L, 2L)
409+
)
410+
)
411+
})
412+
413+
test_that("expand_grid() throws an error for invalid `.vary` parameter", {
414+
expect_snapshot(error = TRUE, {
415+
expand_grid(x = 1:2, y = 1:2, .vary = "invalid")
416+
})
417+
})
418+
393419
# ------------------------------------------------------------------------------
394420
# grid_dots()
395421

0 commit comments

Comments
 (0)