From c346ccbb7cec069e7b90b811985783c05a96c806 Mon Sep 17 00:00:00 2001 From: olivroy Date: Sun, 18 Aug 2024 10:21:37 -0400 Subject: [PATCH] Add extra validation when `.by` is supplied and a group data frame (repeat the error message from mutate() for a better stack trace. --- R/fill.R | 5 +++++ tests/testthat/_snaps/fill.md | 5 +++++ tests/testthat/test-fill.R | 2 ++ 3 files changed, 12 insertions(+) diff --git a/R/fill.R b/R/fill.R index bdd905c0..704f4024 100644 --- a/R/fill.R +++ b/R/fill.R @@ -115,6 +115,11 @@ fill.data.frame <- function(data, ..., .direction = c("down", "up", "downup", "u } if (dplyr::is_grouped_df(data)) { + if (length(by) > 0) { + cli::cli_abort(c( + "Can't supply {.arg .by} when {.arg data} is a grouped data frame." + )) + } dplyr::mutate(data, dplyr::across(any_of(vars), .fns = fn)) } else { dplyr::mutate(data, dplyr::across(any_of(vars), .fns = fn), .by = all_of(by)) diff --git a/tests/testthat/_snaps/fill.md b/tests/testthat/_snaps/fill.md index 314d7cbf..35e90036 100644 --- a/tests/testthat/_snaps/fill.md +++ b/tests/testthat/_snaps/fill.md @@ -14,4 +14,9 @@ Error in `fill()`: ! Can't select columns that don't exist. x Column `z` doesn't exist. + Code + gr_df %>% fill(y, .by = x) + Condition + Error in `fill()`: + ! Can't supply `.by` when `data` is a grouped data frame. diff --git a/tests/testthat/test-fill.R b/tests/testthat/test-fill.R index 48fd1d66..cdd1f82e 100644 --- a/tests/testthat/test-fill.R +++ b/tests/testthat/test-fill.R @@ -134,7 +134,9 @@ test_that("validates its inputs", { test_that("fill works with .by", { df <- tibble(x = c(1, 1, 2), y = c(1, NA, NA)) + gr_df <- dplyr::group_by(df, x) expect_snapshot(error = TRUE, { df %>% fill(y, .by = z) + gr_df %>% fill(y, .by = x) }) })