Skip to content

Commit

Permalink
Update documentation and tests to use where()
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed May 12, 2020
1 parent 8109107 commit f748792
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions R/relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
#' df %>% relocate(a, .after = last_col())
#'
#' # Can also select variables based on their type
#' df %>% relocate(is.character)
#' df %>% relocate(is.numeric, .after = last_col())
#' df %>% relocate(where(is.character))
#' df %>% relocate(where(is.numeric), .after = last_col())
#' # Or with any other select helper
#' df %>% relocate(any_of(c("a", "e", "i", "o", "u")))
#'
#' # When .before or .after refers to multiple variables they will be
#' # moved to be immediately before/after the selected variables.
#' df2 <- tibble(a = 1, b = "a", c = 1, d = "a")
#' df2 %>% relocate(is.numeric, .after = is.character)
#' df2 %>% relocate(is.numeric, .before = is.character)
#' df2 %>% relocate(where(is.numeric), .after = where(is.character))
#' df2 %>% relocate(where(is.numeric), .before = where(is.character))
relocate <- function(.data, ..., .before = NULL, .after = NULL) {
UseMethod("relocate")
}
Expand Down
8 changes: 4 additions & 4 deletions man/relocate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-across-errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
> tibble(x = 1) %>% summarise(res = across(is.numeric, 42))
> tibble(x = 1) %>% summarise(res = across(where(is.numeric), 42))
Error: Problem with `summarise()` input `res`.
x Problem with `across()` input `.fns`.
i Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
i Input `res` is `across(is.numeric, 42)`.
i Input `res` is `across(where(is.numeric), 42)`.

> across()
Error: `across()` must only be used inside dplyr verbs.
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ test_that("across() correctly names output columns", {
c("x", "id_y", "id_z", "id_s")
)
expect_named(
summarise(gf, across(is.numeric, mean)),
summarise(gf, across(where(is.numeric), mean)),
c("x", "y", "z")
)
expect_named(
summarise(gf, across(is.numeric, mean, .names = "mean_{col}")),
summarise(gf, across(where(is.numeric), mean, .names = "mean_{col}")),
c("x", "mean_y", "mean_z")
)
expect_named(
summarise(gf, across(is.numeric, list(mean = mean, sum = sum))),
summarise(gf, across(where(is.numeric), list(mean = mean, sum = sum))),
c("x", "y_mean", "y_sum", "z_mean", "z_sum")
)
expect_named(
summarise(gf, across(is.numeric, list(mean = mean, sum))),
summarise(gf, across(where(is.numeric), list(mean = mean, sum))),
c("x", "y_mean", "y_2", "z_mean", "z_2")
)
expect_named(
summarise(gf, across(is.numeric, list(mean, sum = sum))),
summarise(gf, across(where(is.numeric), list(mean, sum = sum))),
c("x", "y_1", "y_sum", "z_1", "z_sum")
)
expect_named(
summarise(gf, across(is.numeric, list(mean, sum))),
summarise(gf, across(where(is.numeric), list(mean, sum))),
c("x", "y_1", "y_2", "z_1", "z_2")
)
expect_named(
summarise(gf, across(is.numeric, list(mean = mean, sum = sum), .names = "{fn}_{col}")),
summarise(gf, across(where(is.numeric), list(mean = mean, sum = sum), .names = "{fn}_{col}")),
c("x", "mean_y", "sum_y", "mean_z", "sum_z")
)
})
Expand Down Expand Up @@ -89,15 +89,15 @@ test_that("across() avoids simple argument name collisions with ... (#4965)", {
test_that("across() works sequentially (#4907)", {
df <- tibble(a = 1)
expect_equal(
mutate(df, x = ncol(across(is.numeric)), y = ncol(across(is.numeric))),
mutate(df, x = ncol(across(where(is.numeric))), y = ncol(across(is.numeric))),
tibble(a = 1, x = 1L, y = 2L)
)
expect_equal(
mutate(df, a = "x", y = ncol(across(is.numeric))),
mutate(df, a = "x", y = ncol(across(where(is.numeric)))),
tibble(a = "x", y = 0L)
)
expect_equal(
mutate(df, x = 1, y = ncol(across(is.numeric))),
mutate(df, x = 1, y = ncol(across(where(is.numeric)))),
tibble(a = 1, x = 1, y = 2L)
)
})
Expand All @@ -110,7 +110,7 @@ test_that("across() retains original ordering", {
test_that("across() gives meaningful messages", {
verify_output(test_path("test-across-errors.txt"), {
tibble(x = 1) %>%
summarise(res = across(is.numeric, 42))
summarise(res = across(where(is.numeric), 42))

across()
c_across()
Expand All @@ -120,15 +120,15 @@ test_that("across() gives meaningful messages", {
test_that("monitoring cache - across() can be used twice in the same expression", {
df <- tibble(a = 1, b = 2)
expect_equal(
mutate(df, x = ncol(across(is.numeric)) + ncol(across(a))),
mutate(df, x = ncol(across(where(is.numeric))) + ncol(across(a))),
tibble(a = 1, b = 2, x = 3)
)
})

test_that("monitoring cache - across() can be used in separate expressions", {
df <- tibble(a = 1, b = 2)
expect_equal(
mutate(df, x = ncol(across(is.numeric)), y = ncol(across(a))),
mutate(df, x = ncol(across(where(is.numeric))), y = ncol(across(a))),
tibble(a = 1, b = 2, x = 2, y = 1)
)
})
Expand All @@ -155,7 +155,7 @@ test_that("monitoring cache - across() internal cache key depends on all inputs"
df <- group_by(df, g)

expect_identical(
mutate(df, tibble(x = across(is.numeric, mean)$a, y = across(is.numeric, max)$a)),
mutate(df, tibble(x = across(where(is.numeric), mean)$a, y = across(where(is.numeric), max)$a)),
mutate(df, x = mean(a), y = max(a))
)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ test_that(".before and .after relocate individual cols", {

test_that("can move blocks of variables", {
df <- tibble(x = 1, a = "a", y = 2, b = "a")
expect_named(relocate(df, is.character), c("a", "b", "x", "y"))
expect_named(relocate(df, is.character, .after = is.numeric), c("x", "y", "a", "b"))
expect_named(relocate(df, where(is.character)), c("a", "b", "x", "y"))
expect_named(relocate(df, where(is.character), .after = where(is.numeric)), c("x", "y", "a", "b"))
})

test_that("don't lose non-contiguous variables", {
Expand Down

0 comments on commit f748792

Please sign in to comment.