Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tidyr::spread() #552

Merged
merged 5 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/adorn_title.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
sfirke marked this conversation as resolved.
Show resolved Hide resolved
if ("emptystring_" %in% names(result)) {
result <- result[c(setdiff(names(result), "emptystring_"), "emptystring_")]
if (getOption("tabyl.emptystring", TRUE) & interactive()) {
Expand Down
2 changes: 1 addition & 1 deletion man/adorn_title.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-adorn-title.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-adorn-totals.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ 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", {
ct_2 <-
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()
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-tabyl-classifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions vignettes/tabyls.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions vignettes/tabyls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down