Skip to content

Commit

Permalink
Use tidyr new functions (tidyr::gather() -> tidyr::pivot_longer()
Browse files Browse the repository at this point in the history
… and `tidyr::spread()` -> `tidyr::pivot_wider()` )
  • Loading branch information
olivroy committed Jan 20, 2025
1 parent c6e8ec5 commit 0108799
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Suggests:
rvest,
shiny (>= 1.9.1),
testthat (>= 3.1.9),
tidyr,
tidyr (>= 1.0.0),
webshot2 (>= 0.1.0),
withr
Config/Needs/coverage: officer
Expand Down
8 changes: 6 additions & 2 deletions R/data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,11 @@
#' dplyr::filter(latitude == 20 & tst <= "1200") |>
#' dplyr::select(-latitude) |>
#' dplyr::filter(!is.na(sza)) |>
#' tidyr::spread(key = "tst", value = sza) |>
#' tidyr::pivot_wider(
#' names_from = tst,
#' values_from = sza,
#' names_sort = TRUE
#' ) |>
#' gt(rowname_col = "month") |>
#' sub_missing(missing_text = "") |>
#' data_color(
Expand Down Expand Up @@ -679,7 +683,7 @@ data_color <- function(
direction <- rlang::arg_match0(direction, values = c("column", "row"))

# Get the correct `method` value
method <-
method <-
rlang::arg_match0(
method,
values = c("auto", "numeric", "bin", "quantile", "factor")
Expand Down
8 changes: 4 additions & 4 deletions R/fmt_number.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' tidyr::pivot_wider(names_from = year, values_from = population) |>
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_number(suffixing = TRUE)
Expand All @@ -327,7 +327,7 @@
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' tidyr::pivot_wider(names_from = year, values_from = population) |>
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_number(suffixing = TRUE, n_sigfig = 3)
Expand Down Expand Up @@ -676,7 +676,7 @@ fmt_number <- function(
#'
#' @section Compatibility of arguments with the `from_column()` helper function:
#'
#' [from_column()] can be used with certain arguments of `fmt_integer()` to
#' [from_column()] can be used with certain arguments of `fmt_integer()` to
#' obtain varying parameter values from a specified column within the table.
#' This means that each row could be formatted a little bit differently. These
#' arguments provide support for [from_column()]:
Expand Down Expand Up @@ -742,7 +742,7 @@ fmt_number <- function(
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' tidyr::pivot_wider(names_from = year, values_from = population)
#' dplyr::arrange(desc(`2015`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_integer(scale_by = 1 / 1E6) |>
Expand Down
8 changes: 4 additions & 4 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ fmt_partsper <- function(
#

# Ensure that arguments are matched
to_units <-
to_units <-
rlang::arg_match0(
to_units,
values = c("per-mille", "per-myriad", "pcm", "ppm", "ppb", "ppt", "ppq")
Expand Down Expand Up @@ -4240,7 +4240,7 @@ fmt_duration <- function(
stop_if_not_gt_tbl(data = data)

# Ensure that arguments are matched
duration_style <-
duration_style <-
rlang::arg_match0(
duration_style,
values = c("narrow", "wide", "colon-sep", "iso")
Expand Down Expand Up @@ -9665,7 +9665,7 @@ fmt_markdown <- function(
#

# Ensure that arguments are matched
md_engine <-
md_engine <-
rlang::arg_match0(
md_engine,
values = c("markdown", "commonmark")
Expand Down Expand Up @@ -10017,7 +10017,7 @@ fmt_passthrough <- function(
#' dplyr::select(country_code_3, year, population) |>
#' dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
#' dplyr::filter(year > 1975 & year %% 5 == 0) |>
#' tidyr::spread(year, population) |>
#' tidyr::pivot_wider(names_from = year, values_from = population)
#' dplyr::arrange(desc(`2020`)) |>
#' gt(rowname_col = "country_code_3") |>
#' fmt_auto(lg_num_pref = "suf")
Expand Down
6 changes: 5 additions & 1 deletion R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,11 @@ cells_group <- function(groups = everything()) {
#' dplyr::filter(latitude == 20 & tst <= "1000") |>
#' dplyr::select(-latitude) |>
#' dplyr::filter(!is.na(sza)) |>
#' tidyr::spread(key = "tst", value = sza) |>
#' tidyr::pivot_wider(
#' names_from = "tst",
#' values_from = sza,
#' names_sort = TRUE
#' ) |>
#' gt(rowname_col = "month") |>
#' sub_missing(missing_text = "") |>
#' tab_style(
Expand Down
14 changes: 10 additions & 4 deletions data-raw/01-countrypops.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ countrypops <-
`2021` = col_integer(),
`2022` = col_integer(),
`2023` = col_integer()
), na = character()
),
na = ""
) %>%
tidyr::gather("year", "population", `1960`:`2023`) %>%
dplyr::arrange(country_code_3, year) %>%
dplyr::mutate(year = as.integer(year))
tidyr::pivot_longer(
cols = `1960`:`2023`,
names_to = "year",
# transform year to integer
names_transform = as.integer,
values_to = "population"
) %>%
dplyr::arrange(country_code_3, year)
7 changes: 5 additions & 2 deletions data-raw/05-pizzaplace.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
library(tidyverse)
library(lubridate)
library(progress)

# Define the pizza names, types, available sizes, and prices for each;
Expand Down Expand Up @@ -503,7 +502,11 @@ orders_year <-
daily_random_orders =
sample(floor(rnorm(100, mean = 15, sd = 4)), n(), replace = TRUE)
) %>%
tidyr::gather("order_type", "n", grp_ord_n:daily_random_orders) %>%
tidyr::pivot_longer(
cols = grp_ord_n:daily_random_orders,
names_to = "order_type",
values_to = "n"
) %>%
dplyr::arrange(date, order_type) %>%
dplyr::filter(!(date %in% full_closures)) %>%
apply_exceptional_days(exceptional_df = exceptional_tbl)
Expand Down
6 changes: 5 additions & 1 deletion man/cells_stub.Rd

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

6 changes: 5 additions & 1 deletion man/data_color.Rd

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

2 changes: 1 addition & 1 deletion man/fmt_auto.Rd

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

4 changes: 2 additions & 2 deletions man/fmt_integer.Rd

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

4 changes: 2 additions & 2 deletions man/fmt_number.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ test_that("The direction of coloring can be column-wise or row-wise", {
dplyr::filter(latitude == 20, tst <= "1200") %>%
dplyr::select(-latitude) %>%
dplyr::filter(!is.na(sza)) %>%
tidyr::spread(key = "tst", value = sza) %>%
tidyr::pivot_wider(names_from = "tst", values_from = sza, names_sort = TRUE) %>%
gt(rowname_col = "month") %>%
sub_missing(missing_text = "")

Expand Down

0 comments on commit 0108799

Please sign in to comment.