Skip to content

Commit

Permalink
Drop dplyr and sjmisc deps
Browse files Browse the repository at this point in the history
import magrittr pipe through gt
make gt a strong dep
  • Loading branch information
mrcaseb committed Jan 24, 2024
1 parent 2af0ecc commit ae87ba9
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: A set of functions to visualize National Football League
Expand All @@ -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),
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions R/nfl_team_factors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) +
Expand All @@ -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]])

Expand Down
30 changes: 11 additions & 19 deletions R/nfl_team_tiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))){
Expand All @@ -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)
Expand All @@ -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) +
Expand Down
2 changes: 1 addition & 1 deletion R/nflplotR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 10 additions & 8 deletions man/element.Rd

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

4 changes: 1 addition & 3 deletions man/nfl_team_factor.Rd

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

13 changes: 6 additions & 7 deletions man/nfl_team_tiers.Rd

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

14 changes: 5 additions & 9 deletions tests/testthat/test-nfl_team_tiers.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit ae87ba9

Please sign in to comment.