Skip to content

Commit

Permalink
Globals & namespace #47 (#71)
Browse files Browse the repository at this point in the history
- add stats to 'Suggests'
- add global vars
- fix namespace issues
- add port of shift.column into utils instead of importing useful package
- remove useful from imports and namespace
- remove Rcpp and RcppRoll from imports (unused)

closes #47
  • Loading branch information
Ryo-N7 authored Jun 20, 2022
1 parent bf2e8bd commit 8f555d8
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 20 deletions.
8 changes: 3 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ Imports:
ggsoccer,
ggrepel,
ggtext,
Rcpp,
RcppRoll,
TTR,
stringi,
forcats,
purrr,
ggforce,
gridExtra,
useful
gridExtra
Suggests:
rmarkdown,
knitr,
testthat (>= 3.0.0),
covr
covr,
stats
Config/testthat/edition: 3
Depends:
R (>= 3.5.0)
Expand Down
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export(plot_sonar)
export(plot_timeline)
export(plot_trendline)
export(plot_voronoi)
import(Rcpp)
import(RcppRoll)
import(TTR)
import(dplyr)
import(ggforce)
Expand All @@ -25,7 +23,6 @@ import(ggsoccer)
import(ggtext)
import(purrr)
import(tidyr)
import(useful)
importFrom(dplyr,filter)
importFrom(dplyr,slice)
importFrom(forcats,fct_reorder)
Expand All @@ -36,9 +33,12 @@ importFrom(ggplot2,geom_segment)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(grDevices,chull)
importFrom(gridExtra,tableGrob)
importFrom(gridExtra,ttheme_minimal)
importFrom(magrittr,"%>%")
importFrom(stats,na.omit)
importFrom(stats,quantile)
importFrom(stringi,stri_c)
importFrom(stringi,stri_trans_general)
importFrom(stringi,stri_wrap)
6 changes: 5 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ utils::globalVariables(
"N", "n.angle", "maxN", "angle.norm", "pass.length", "frequency", "year",
"season", "h_a", "home_team", "away_team", "home_away", "minute", "xGsum",
"complete.cases", "Home", "Away", "Away_xG", "xGA", "xGSUM", "Date",
"xGSM", "xGASM", "Home_xG", "distance"))
"xGSM", "xGASM", "Home_xG", "distance",
"events", "from", "outcome", "pass.recipient.name",
"playerId", "player_label", "receiver", "teamId",
"to", "type", "type.name", "xT", "xTEnd",
"xTStart", "xend", "yend"))
4 changes: 2 additions & 2 deletions R/plot_passnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @import tidyr
#' @import ggsoccer
#' @importFrom gridExtra tableGrob ttheme_minimal
#' @import useful
#' @importFrom stats na.omit
#'
#' @export
#'
Expand Down Expand Up @@ -270,7 +270,7 @@ plot_passnet <- function(data, data_type = "statsbomb", team_name, scale_stat =
filter(teamId == team_name)

data1 <- data1[complete.cases(data1[, "playerId"]), ]
data1 <- shift.column(data = data1, columns = "playerId", newNames = "receiver", len = 1, up = TRUE)
data1 <- shift_column(data = data1, columns = "playerId", new_names = "receiver", len = 1, up = TRUE)

# Filter to before first substitution

Expand Down
2 changes: 0 additions & 2 deletions R/plot_trendline.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#' @import dplyr
#' @import ggplot2
#' @import ggtext
#' @import Rcpp
#' @import RcppRoll
#' @import TTR
#'
#' @export
Expand Down
62 changes: 55 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,91 @@
#' @importFrom stringi stri_wrap stri_c

text_wrap <- function(x) {
wrapped_text <- stringi::stri_wrap(x, width = 10, whitespace_only = TRUE, simplify = FALSE)
final_text <- vapply(wrapped_text, stringi::stri_c, collapse = "\n", character(1))
wrapped_text <- stri_wrap(x, width = 10, whitespace_only = TRUE, simplify = FALSE)
final_text <- vapply(wrapped_text, stri_c, collapse = "\n", character(1))

return(final_text)
}


#' @title shift_column
#' @description Port of `shift.column()` from Jared Lander's {useful} package
#' with a few minor changes.
#' @param data data
#' @param columns columns to shift over
#' @param new_names New name of shifted columns, Default: sprintf("%s.Shifted", columns)
#' @param len length of rows to shift, Default: 1
#' @param up shift rows up (TRUE) or down (FALSE), Default: TRUE
#' @return data.frame with shifted columns
#' @keywords internal
#' @rdname shift_column

shift_column <- function(data, columns, new_names = sprintf("%s.Shifted", columns), len = 1L, up = TRUE) {
if (length(columns) != length(new_names)) {
stop("columns and new_names must be the same length")
}

# get the rows to keep based on how much to shift it by and weather to shift up or down
rowsToKeep <- seq(from = 1 + len * up, length.out = NROW(data) - len)

# for the original data -- it needs to be shifted the other way
dataRowsToKeep <- seq(from = 1 + len * !up, length.out = NROW(data) - len)

#create a df of the shifted rows
shiftedDF <- data[rowsToKeep, columns]

# give the right names to these new columns
names(shiftedDF) <- new_names

# data names
dataNames <- names(data)

# get rid of excess rows in data
data <- data[dataRowsToKeep, ]

# tack shifted data onto the end of the original (and cutoff) data
data <- cbind(data, shiftedDF)
names(data) <- c(dataNames, new_names)

return(data)
}


#' @title hull_fun
#' @description Create boundaries for convex hull
#' @param x data frame
#' @return data frame
#' @rdname hull_fun
#' @keywords internal
#' @importFrom dplyr filter slice
hull_fun <- function(data) {
#' @importFrom grDevices chull
#' @importFrom stats quantile

hull_fun <- function(data) {

x_low <- quantile(data$x, 0.05)
x_high <- quantile(data$x, 0.95)

y_low <- quantile(data$y, 0.05)
y_high <- quantile(data$y, 0.95)

hull_data <- data %>%
filter((x > x_low) & (x < x_high)) %>%
filter((y > y_low) & (y < y_high)) %>%
slice(chull(x, y))

return(hull_data)
}


#' title zissou_pal
#' @description 'Zissou' palette hex codes. Useed in `plot_sonar()` function.
#' Created by Karthik Ram in the {wesanderson} R package.
#' @keywords internal

zissou_pal <- c("#3B9AB2", "#56A6BA", "#71B3C2", "#9EBE91", "#D1C74C",
"#E8C520", "#E4B80E", "#E29E00", "#EA5C00", "#F21A00")
"#E8C520", "#E4B80E", "#E29E00", "#EA5C00", "#F21A00")


#' @title viridis_d_pal
#' @description Viridis palette hex codes. Used in `plot_passflow()` function.
Expand Down
33 changes: 33 additions & 0 deletions man/shift_column.Rd

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

0 comments on commit 8f555d8

Please sign in to comment.