.datatable.aware <- TRUE
#' Progressively
#'
#' This function helps add progress-reporting to any function - given function `f()` and progressor `p()`, it will return a new function that calls `f()` and then (on-exiting) will call `p()` after every iteration.
#'
#' This is inspired by purrr's `safely`, `quietly`, and `possibly` function decorators.
#'
#' @param f a function to add progressr functionality to.
#' @param p a progressor function as created by `progressr::progressor()`
#' @keywords Internal
#'
#' @return a function that does the same as `f` but it calls `p()` after iteration.
#'
progressively <- function(f, p = NULL){
if (!is.null(p) && !inherits(p, "progressor")) stop("`p` must be a progressor function!")
if (is.null(p)) p <- function(...) NULL
force(f)
function(...){
on.exit(p("loading..."))
f(...)
}
}
#' @title
#' **Load .csv / .csv.gz file from a remote connection**
#' @description
#' This is a thin wrapper on data.table::fread
#' @param ... passed to data.table::fread
#' @keywords Internal
#' @importFrom data.table fread
csv_from_url <- function(...){
data.table::fread(...)
}
#' @title
#' **Load .rds file from a remote connection**
#' @param url a character url
#' @keywords Internal
#' @return a dataframe as created by [`readRDS()`]
#' @importFrom data.table data.table setDT
#' @import rvest
rds_from_url <- function(url) {
con <- url(url)
on.exit(close(con))
load <- try(readRDS(con), silent = TRUE)
if (inherits(load, "try-error")) {
warning(paste0("Failed to readRDS from <", url, ">"), call. = FALSE)
return(data.table::data.table())
}
data.table::setDT(load)
return(load)
}
# The function `message_completed` to create the green "...completed" message
# only exists to hide the option `in_builder` in dots
message_completed <- function(x, in_builder = FALSE) {
if (!in_builder) {
usethis::ui_done("{usethis::ui_field(x)}")
} else if (in_builder) {
usethis::ui_done(x)
}
}
user_message <- function(x, type) {
if (type == "done") {
usethis::ui_done("{my_time()} | {x}")
} else if (type == "todo") {
usethis::ui_todo("{my_time()} | {x}")
} else if (type == "info") {
usethis::ui_info("{my_time()} | {x}")
} else if (type == "oops") {
usethis::ui_oops("{my_time()} | {x}")
}
}
# check if a package is installed
is_installed <- function(pkg) requireNamespace(pkg, quietly = TRUE)
# custom mode function from https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode/8189441
custom_mode <- function(x, na.rm = TRUE) {
if (na.rm) {
x <- x[!is.na(x)]
}
ux <- unique(x)
return(ux[which.max(tabulate(match(x, ux)))])
}
#' @title
#' **Most Recent Women's College Basketball Season**
#' @export
most_recent_wbb_season <- function() {
ifelse(
as.double(substr(Sys.Date(), 6, 7)) >= 10,
as.double(substr(Sys.Date(), 1, 4)) + 1,
as.double(substr(Sys.Date(), 1, 4))
)
}
#' @title
#' **Most Recent WNBA Season**
#' @export
most_recent_wnba_season <- function() {
ifelse(
as.double(substr(Sys.Date(), 6, 7)) >= 5,
as.double(substr(Sys.Date(), 1, 4)),
as.double(substr(Sys.Date(), 1, 4)) - 1
)
}
my_time <- function() strftime(Sys.time(), format = "%H:%M:%S")
#' Check Status function
#' @param res Response from API
#' @keywords Internal
#' @import rvest
#'
check_status <- function(res) {
x = httr::status_code(res)
if (x != 200) stop("The API returned an error", call. = FALSE)
}
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
#' @import utils
utils::globalVariables(c("where"))
# check if a package is installed
is_installed <- function(pkg) requireNamespace(pkg, quietly = TRUE)
#' @keywords internal
"_PACKAGE"
#' @importFrom Rcpp getRcppVersion
#' @importFrom RcppParallel defaultNumThreads
NULL
`%c%` <- function(x,y){
ifelse(!is.na(x),x,y)
}
# Functions for custom class
# turn a data.frame into a tibble/wehoop_data
make_wehoop_data <- function(df, type, timestamp){
out <- df %>%
tidyr::as_tibble()
class(out) <- c("wehoop_data","tbl_df","tbl","data.table","data.frame")
attr(out,"wehoop_timestamp") <- timestamp
attr(out,"wehoop_type") <- type
return(out)
}
#' @export
#' @noRd
print.wehoop_data <- function(x,...) {
cli::cli_rule(left = "{attr(x,'wehoop_type')}",right = "{.emph wehoop {utils::packageVersion('wehoop')}}")
if (!is.null(attr(x,'wehoop_timestamp'))) {
cli::cli_alert_info(
"Data updated: {.field {format(attr(x,'wehoop_timestamp'), tz = Sys.timezone(), usetz = TRUE)}}"
)
}
NextMethod(print,x)
invisible(x)
}
# rbindlist but maintain attributes of last file
rbindlist_with_attrs <- function(dflist){
wehoop_timestamp <- attr(dflist[[length(dflist)]], "wehoop_timestamp")
wehoop_type <- attr(dflist[[length(dflist)]], "wehoop_type")
out <- data.table::rbindlist(dflist, use.names = TRUE, fill = TRUE)
attr(out,"wehoop_timestamp") <- wehoop_timestamp
attr(out,"wehoop_type") <- wehoop_type
class(out) <- c("wehoop_data","tbl_df","tbl","data.table","data.frame")
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.