From ae87ba9f20955fa1346869a8135756214c2e885a Mon Sep 17 00:00:00 2001 From: Sebastian Carl Date: Wed, 24 Jan 2024 13:41:32 +0100 Subject: [PATCH] Drop dplyr and sjmisc deps import magrittr pipe through gt make gt a strong dep --- .github/workflows/pkgdown.yaml | 2 +- DESCRIPTION | 7 ++----- NAMESPACE | 2 +- R/nfl_team_factors.R | 6 ++---- R/nfl_team_tiers.R | 30 ++++++++++------------------ R/nflplotR-package.R | 2 +- R/theme-elements.R | 18 +++++++++-------- man/element.Rd | 18 +++++++++-------- man/nfl_team_factor.Rd | 4 +--- man/nfl_team_tiers.Rd | 13 ++++++------ tests/testthat/test-nfl_team_tiers.R | 14 +++++-------- 11 files changed, 50 insertions(+), 66 deletions(-) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index e913f04..1980a3d 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -33,7 +33,7 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: r-lib/pkgdown, local::. + extra-packages: r-lib/pkgdown, local::., any::dplyr needs: website - name: Build site diff --git a/DESCRIPTION b/DESCRIPTION index 75c7532..766ac3e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nflplotR Title: NFL Logo Plots in 'ggplot2' -Version: 1.2.0.9006 +Version: 1.2.0.9007 Authors@R: person("Sebastian", "Carl", , "mrcaseb@gmail.com", role = c("aut", "cre")) Description: A set of functions to visualize National Football League @@ -17,9 +17,9 @@ Imports: ggpath (>= 1.0.1), ggplot2 (>= 3.3.0), grid, + gt (>= 0.8.0), lifecycle, magick (>= 2.7.3), - magrittr (>= 2.0.0), memoise (>= 2.0.0), nflreadr (>= 1.3.2), rappdirs (>= 0.3.0), @@ -28,15 +28,12 @@ Imports: Suggests: base64enc (>= 0.1-3), covr, - dplyr (>= 1.0.0), ggtext (>= 0.1.1), gridtext (>= 0.1.4), - gt (>= 0.8.0), knitr, rmarkdown, rstudioapi (>= 0.13), rsvg (>= 2.0), - sjmisc (>= 2.8.7), testthat (>= 3.0.0), vdiffr (>= 1.0.2), webshot2 (>= 0.1.0) diff --git a/NAMESPACE b/NAMESPACE index ff78326..51916a6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -42,8 +42,8 @@ import(data.table) import(grid) importFrom(ggplot2,element_grob) importFrom(graphics,par) +importFrom(gt,"%>%") importFrom(lifecycle,deprecated) -importFrom(magrittr,"%>%") importFrom(nflreadr,nflverse_sitrep) importFrom(rlang,"%||%") importFrom(rlang,.data) diff --git a/R/nfl_team_factors.R b/R/nfl_team_factors.R index 472af3d..3707e33 100644 --- a/R/nfl_team_factors.R +++ b/R/nfl_team_factors.R @@ -28,7 +28,6 @@ #' ######### HOW TO USE IN PRACTICE ######### #' #' library(ggplot2) -#' library(magrittr) #' # load some sample data from the ggplot2 package #' plot_data <- mpg #' # add a new column by randomly sampling the above defined teams vector @@ -44,8 +43,7 @@ #' # We'll change the order of facets by making another team name column and #' # converting it to an ordered factor. Again, this defaults to sort by division #' # and nick name in ascending order. -#' plot_data$ordered_team <- sample(teams, nrow(mpg), replace = TRUE) %>% -#' nfl_team_factor() +#' plot_data$ordered_team <- nfl_team_factor(sample(teams, nrow(mpg), replace = TRUE)) #' #' # Let's check how the facets are ordered now. #' ggplot(plot_data, aes(displ, hwy)) + @@ -68,7 +66,7 @@ nfl_team_factor <- function(teams, ...){ n_args <- rlang::dots_n(...) # load nflreadr teams and make it a data.table - nfl_teams <- nflreadr::load_teams() %>% data.table::setDT() + nfl_teams <- data.table::setDT(nflreadr::load_teams()) div_split <- data.table::tstrsplit(nfl_teams$team_division, " ") nfl_teams$team_division_rev <- paste(div_split[[2]], div_split[[1]]) diff --git a/R/nfl_team_tiers.R b/R/nfl_team_tiers.R index f1660a8..fe841d3 100644 --- a/R/nfl_team_tiers.R +++ b/R/nfl_team_tiers.R @@ -35,19 +35,18 @@ #' @examples #' \donttest{ #' library(ggplot2) -#' library(dplyr, warn.conflicts = FALSE) -#' teams <- valid_team_names() +#' library(data.table) +#' teams <- nflplotR::valid_team_names() #' # remove conference logos from this example #' teams <- teams[!teams %in% c("AFC", "NFC", "NFL")] +#' teams <- sample(teams) #' -#' # Build the team tiers data frame +#' # Build the team tiers data #' # This is completely random! -#' df <- data.frame( +#' dt <- data.table::data.table( #' tier_no = sample(1:5, length(teams), replace = TRUE), #' team_abbr = teams -#' ) %>% -#' dplyr::group_by(tier_no) %>% -#' dplyr::mutate(tier_rank = sample(1:n(), n())) +#' )[,tier_rank := sample(1:.N, .N), by = "tier_no"] #' #' # Plot team tiers #' nfl_team_tiers(df) @@ -97,8 +96,6 @@ nfl_team_tiers <- function(data, no_line_below_tier = NULL, devel = FALSE){ - rlang::check_installed("sjmisc", "to build the nflplotR team tiers.") - required_vars <- c("tier_no", "team_abbr") if (!all(required_vars %in% names(data))){ @@ -113,18 +110,13 @@ nfl_team_tiers <- function(data, tierlines <- c(min(tiers) - 0.5, tierlines) if (isTRUE(presort)){ - data <- data %>% - dplyr::group_by(.data$tier_no) %>% - dplyr::arrange(.data$team_abbr) %>% - dplyr::mutate(tier_rank = 1:dplyr::n()) %>% - dplyr::ungroup() + data <- data.table::as.data.table(data)[order(tier_no, team_abbr)] + data[, tier_rank := 1:.N, by = "tier_no"] } if (!"tier_rank" %in% names(data)){ - data <- data %>% - dplyr::group_by(.data$tier_no) %>% - dplyr::mutate(tier_rank = 1:dplyr::n()) %>% - dplyr::ungroup() + data <- data.table::as.data.table(data) + data[, tier_rank := 1:.N, by = "tier_no"] } data$team_abbr <- nflreadr::clean_team_abbrs(as.character(data$team_abbr), keep_non_matches = FALSE) @@ -140,7 +132,7 @@ nfl_team_tiers <- function(data, expand = ggplot2::expansion(add = 0.1), limits = rev(c(min(tiers) - 0.5, max(tiers) + 0.5)), breaks = rev(tiers), - labels = function(x) sjmisc::word_wrap(tier_desc[x], 15), + labels = function(x) scales::label_wrap(15)(tier_desc[x]), trans = "reverse" ) + ggplot2::labs(title = title, subtitle = subtitle, caption = caption) + diff --git a/R/nflplotR-package.R b/R/nflplotR-package.R index 629bcf1..238df29 100644 --- a/R/nflplotR-package.R +++ b/R/nflplotR-package.R @@ -9,7 +9,7 @@ #' @importFrom ggplot2 element_grob #' @importFrom graphics par #' @importFrom lifecycle deprecated -#' @importFrom magrittr %>% +#' @importFrom gt %>% #' @importFrom rlang .data %||% #' @importFrom utils capture.output ## usethis namespace: end diff --git a/R/theme-elements.R b/R/theme-elements.R index f19c2ac..dec815a 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -101,16 +101,18 @@ #' #' library(ggplot2) #' -#' df <- dplyr::mutate(mtcars, -#' team = sample(c("LAC", "BUF", "DAL", "ARI"), nrow(mtcars), TRUE), -#' player = sample( -#' c("00-0033873", "00-0035228", "00-0036355", "00-0019596"), -#' nrow(mtcars), -#' TRUE +#' dt <- data.table::as.data.table(mtcars)[, +#' `:=`( +#' team = sample(c("LAC", "BUF", "DAL", "ARI"), nrow(mtcars), TRUE), +#' player = sample( +#' c("00-0033873", "00-0035228", "00-0036355", "00-0019596"), +#' nrow(mtcars), +#' TRUE +#' ) #' ) -#' ) +#' ] #' -#' ggplot(df, aes(x = mpg, y = disp)) + +#' ggplot(dt, aes(x = mpg, y = disp)) + #' geom_point() + #' facet_wrap(vars(team)) + #' labs( diff --git a/man/element.Rd b/man/element.Rd index 15a0c80..3226c45 100644 --- a/man/element.Rd +++ b/man/element.Rd @@ -152,16 +152,18 @@ options(old) library(ggplot2) -df <- dplyr::mutate(mtcars, - team = sample(c("LAC", "BUF", "DAL", "ARI"), nrow(mtcars), TRUE), - player = sample( - c("00-0033873", "00-0035228", "00-0036355", "00-0019596"), - nrow(mtcars), - TRUE +dt <- data.table::as.data.table(mtcars)[, + `:=`( + team = sample(c("LAC", "BUF", "DAL", "ARI"), nrow(mtcars), TRUE), + player = sample( + c("00-0033873", "00-0035228", "00-0036355", "00-0019596"), + nrow(mtcars), + TRUE + ) ) -) +] -ggplot(df, aes(x = mpg, y = disp)) + +ggplot(dt, aes(x = mpg, y = disp)) + geom_point() + facet_wrap(vars(team)) + labs( diff --git a/man/nfl_team_factor.Rd b/man/nfl_team_factor.Rd index f35e069..5ae0fac 100644 --- a/man/nfl_team_factor.Rd +++ b/man/nfl_team_factor.Rd @@ -40,7 +40,6 @@ nfl_team_factor(teams, rev(team_abbr)) ######### HOW TO USE IN PRACTICE ######### library(ggplot2) -library(magrittr) # load some sample data from the ggplot2 package plot_data <- mpg # add a new column by randomly sampling the above defined teams vector @@ -56,8 +55,7 @@ ggplot(plot_data, aes(displ, hwy)) + # We'll change the order of facets by making another team name column and # converting it to an ordered factor. Again, this defaults to sort by division # and nick name in ascending order. -plot_data$ordered_team <- sample(teams, nrow(mpg), replace = TRUE) \%>\% - nfl_team_factor() +plot_data$ordered_team <- nfl_team_factor(sample(teams, nrow(mpg), replace = TRUE)) # Let's check how the facets are ordered now. ggplot(plot_data, aes(displ, hwy)) + diff --git a/man/nfl_team_tiers.Rd b/man/nfl_team_tiers.Rd index c7e5a34..8b23831 100644 --- a/man/nfl_team_tiers.Rd +++ b/man/nfl_team_tiers.Rd @@ -63,19 +63,18 @@ This function sets up a ggplot to visualize NFL team tiers. \examples{ \donttest{ library(ggplot2) -library(dplyr, warn.conflicts = FALSE) -teams <- valid_team_names() +library(data.table) +teams <- nflplotR::valid_team_names() # remove conference logos from this example teams <- teams[!teams \%in\% c("AFC", "NFC", "NFL")] +teams <- sample(teams) -# Build the team tiers data frame +# Build the team tiers data # This is completely random! -df <- data.frame( +dt <- data.table::data.table( tier_no = sample(1:5, length(teams), replace = TRUE), team_abbr = teams -) \%>\% - dplyr::group_by(tier_no) \%>\% - dplyr::mutate(tier_rank = sample(1:n(), n())) +)[,tier_rank := sample(1:.N, .N), by = "tier_no"] # Plot team tiers nfl_team_tiers(df) diff --git a/tests/testthat/test-nfl_team_tiers.R b/tests/testthat/test-nfl_team_tiers.R index 18f983f..bfb4892 100644 --- a/tests/testthat/test-nfl_team_tiers.R +++ b/tests/testthat/test-nfl_team_tiers.R @@ -1,21 +1,17 @@ test_that("team tiers work", { library(ggplot2) - library(dplyr, warn.conflicts = FALSE) - teams <- valid_team_names() + teams <- nflplotR::valid_team_names() # remove conference logos from this example teams <- teams[!teams %in% c("AFC", "NFC", "NFL")] - set.seed(32) - # Build the team tiers data frame + teams <- sample(teams) + # Build the team tiers data # This is completely random! - df <- data.frame( + dt <- data.table::data.table( tier_no = sample(1:5, length(teams), replace = TRUE), team_abbr = teams - ) %>% - dplyr::group_by(tier_no) %>% - dplyr::mutate(tier_rank = sample(1:n(), n())) %>% - dplyr::ungroup() + )[,tier_rank := sample(1:.N, .N), by = "tier_no"] # Check dev mode only because logos are tested elsewhere p1 <- nfl_team_tiers(df,