diff --git a/DESCRIPTION b/DESCRIPTION index fad20f4f..08153716 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,7 +32,7 @@ Imports: stringr, snakecase (>= 0.9.2), tidyselect (>= 1.0.0), - tidyr (>= 0.7.0) + tidyr (>= 1.0.0) License: MIT + file LICENSE LazyData: true RoxygenNote: 7.2.3 diff --git a/R/adorn_title.R b/R/adorn_title.R index 273d813b..3294cb29 100644 --- a/R/adorn_title.R +++ b/R/adorn_title.R @@ -22,7 +22,7 @@ #' mtcars %>% #' group_by(gear, am) %>% #' summarise(avg_mpg = mean(mpg), .groups = "drop") %>% -#' spread(gear, avg_mpg) %>% +#' pivot_wider(names_from = am, values_from = avg_mpg) %>% #' adorn_rounding() %>% #' adorn_title("top", row_name = "Gears", col_name = "Cylinders") adorn_title <- function(dat, placement = "top", row_name, col_name) { diff --git a/R/tabyl.R b/R/tabyl.R index 11d020b2..03576757 100644 --- a/R/tabyl.R +++ b/R/tabyl.R @@ -207,7 +207,12 @@ tabyl_2way <- function(dat, var1, var2, show_na = TRUE, show_missing_levels = TR tabl[2][is.na(tabl[2])] <- "NA_" tabl[2][tabl[2] == ""] <- "emptystring_" result <- tabl %>% - tidyr::spread(!!var2, "tabyl_2way_n", fill = 0) + tidyr::pivot_wider( + names_from = !!var2, + values_from = "tabyl_2way_n", + values_fn = ~ dplyr::coalesce(.x, 0L), + names_sort = TRUE + ) if ("emptystring_" %in% names(result)) { result <- result[c(setdiff(names(result), "emptystring_"), "emptystring_")] if (getOption("tabyl.emptystring", TRUE) & interactive()) { diff --git a/man/adorn_title.Rd b/man/adorn_title.Rd index f6f9d092..81c17b14 100644 --- a/man/adorn_title.Rd +++ b/man/adorn_title.Rd @@ -33,7 +33,7 @@ library(dplyr) mtcars \%>\% group_by(gear, am) \%>\% summarise(avg_mpg = mean(mpg), .groups = "drop") \%>\% - spread(gear, avg_mpg) \%>\% + pivot_wider(names_from = am, values_from = avg_mpg) \%>\% adorn_rounding() \%>\% adorn_title("top", row_name = "Gears", col_name = "Cylinders") } diff --git a/tests/testthat/test-adorn-title.R b/tests/testthat/test-adorn-title.R index d6d959ba..d0e2d3ec 100644 --- a/tests/testthat/test-adorn-title.R +++ b/tests/testthat/test-adorn-title.R @@ -38,7 +38,7 @@ test_that("name overrides work", { test_that("non-tabyls are treated correctly", { non_tab <- mtcars %>% dplyr::count(gear, cyl) %>% - tidyr::spread(gear, n) + tidyr::pivot_wider(names_from = gear, values_from = n) expect_error(adorn_title(non_tab), "When input is not a data.frame of class tabyl, a value must be specified for the col_name argument") expect_equal( @@ -111,7 +111,7 @@ test_that("for printing purposes: tabyl class stays tabyl, data.frame stays data mtcars %>% dplyr::group_by(cyl, am) %>% dplyr::summarise(mean_mpg = mean(mpg)) %>% - tidyr::spread(am, mean_mpg) + tidyr::pivot_wider(names_from = am, values_from = mean_mpg) # handles tibble input expect_s3_class( diff --git a/tests/testthat/test-adorn-totals.R b/tests/testthat/test-adorn-totals.R index 9046ce47..b7598ebc 100644 --- a/tests/testthat/test-adorn-totals.R +++ b/tests/testthat/test-adorn-totals.R @@ -120,7 +120,7 @@ ct_2 <- mtcars %>% dplyr::group_by(cyl, gear) %>% dplyr::tally() %>% - tidyr::spread(gear, n) + tidyr::pivot_wider(names_from = gear, values_from = n) df1 <- data.frame(x = c(1, 2), y = c(NA, 4)) test_that("grouped_df gets ungrouped and succeeds", { @@ -128,7 +128,7 @@ test_that("grouped_df gets ungrouped and succeeds", { mtcars %>% dplyr::group_by(cyl, gear) %>% dplyr::tally() %>% - tidyr::spread(gear, n) + tidyr::pivot_wider(names_from = gear, values_from = n) expect_equal( ct_2 %>% adorn_totals(), ct_2 %>% dplyr::ungroup() %>% adorn_totals() diff --git a/tests/testthat/test-tabyl-classifiers.R b/tests/testthat/test-tabyl-classifiers.R index 2279f993..f682d9e4 100644 --- a/tests/testthat/test-tabyl-classifiers.R +++ b/tests/testthat/test-tabyl-classifiers.R @@ -5,11 +5,16 @@ a <- mtcars %>% b <- mtcars %>% dplyr::count(cyl, carb) %>% - tidyr::spread(carb, n, fill = 0) %>% + tidyr::pivot_wider( + names_from = carb, + values_from = n, + values_fill = 0, + names_sort = TRUE + ) %>% as.data.frame() # for comparison purposes, remove the tbl_df aspect -test_that("as_tabyl works on result of a non-janitor count/spread", { +test_that("as_tabyl works on result of a non-janitor count/pivot_wider", { expect_equal( as_tabyl(a), as_tabyl(b, 2, "cyl", "carb") diff --git a/vignettes/tabyls.Rmd b/vignettes/tabyls.Rmd index 3382608a..31908572 100644 --- a/vignettes/tabyls.Rmd +++ b/vignettes/tabyls.Rmd @@ -208,11 +208,11 @@ cases %>% Here's a more complex example that uses a data.frame of means, not counts. We create a table containing the mean of a 3rd variable when grouped by two other variables, then use `adorn_` functions to round the values and append Ns. The first part is pretty straightforward: ```{r more_non_tabyls, warning = FALSE, message = FALSE} -library(tidyr) # for spread() +library(tidyr) # for pivot_wider() mpg_by_cyl_and_am <- mtcars %>% group_by(cyl, am) %>% summarise(mpg = mean(mpg), .groups = "drop") %>% - spread(am, mpg) + pivot_wider(names_from = am, values_from = mpg) mpg_by_cyl_and_am ``` diff --git a/vignettes/tabyls.md b/vignettes/tabyls.md index 5da88717..ea526931 100644 --- a/vignettes/tabyls.md +++ b/vignettes/tabyls.md @@ -353,11 +353,11 @@ grouped by two other variables, then use `adorn_` functions to round the values and append Ns. The first part is pretty straightforward: ``` r -library(tidyr) # for spread() +library(tidyr) # for pivot_wider() mpg_by_cyl_and_am <- mtcars %>% group_by(cyl, am) %>% summarise(mpg = mean(mpg), .groups = "drop") %>% - spread(am, mpg) + pivot_wider(names_from = am, values_from = mpg) mpg_by_cyl_and_am #> # A tibble: 3 × 3