Skip to content

Instantly share code, notes, and snippets.

View tonyelhabr's full-sized avatar
🏠
Working from home

Tony ElHabr tonyelhabr

🏠
Working from home
View GitHub Profile
@tonyelhabr
tonyelhabr / grid_dates.R
Last active February 13, 2020 17:17
`expand.grid()` for years and months
library(tidyverse)
# grid_ym ----
.years <- 2017:2018
.months <- 1:12
grid_ym <-
crossing(
year = .years,
month = .months
@tonyelhabr
tonyelhabr / install_pkgs_newenv.R
Last active May 5, 2020 23:52
Installing R package on a new desktop/laptop.
dir_pkgs_rds <- 'c:/users/aelhabr/desktop'
r_version_prev <- '3.5'
r_version_new <- '4.0'
path_rds_prev <- file.path(dir_pkgs_rds, sprintf('pkgs_%s.rds', r_version_prev))
dir_pkgs_installed <- 'c:/users/aelhabr/documents/r/win-library'
# ----
# + Open RStudio.
# + Run the top-level lines.
@tonyelhabr
tonyelhabr / utils-display.R
Last active October 10, 2019 12:12
R functions for displaying information, warnings, and errors.
.verbose <- TRUE
.display_info <- function(x, ..., .envir = parent.frame(), verbose = .verbose) {
if(!verbose) {
return(invisible(NULL))
}
# usethis::ui_line(glue::glue('{x}'), .envir = .envir)
# Reference: `usethis::ui_line()`
x <- glue::glue_collapse(x, '\n')
x <- glue::glue(x, .envir = .envir)
@tonyelhabr
tonyelhabr / utils-files.R
Last active October 10, 2019 12:49
R functions for files.
# NOTE: Most of these are from my {vp2} project, which builds upon other past work.
file_path <- function(...) {
file.path(..., fsep = '/')
}
# Reference: https://github.com/tonyelhabr/nba-rapm/blob/master/R/files.R
.create_dir_ifnecessary <-
function(dir, ...) {
if(!dir.exists(dir)) {
@tonyelhabr
tonyelhabr / utils-validate.R
Last active October 10, 2019 12:08
Custom R functions for validating and coercing.
# NOTE: Most of these are from my {vp2} project, which builds upon other past work.
.validate_path <- function(x, strict = TRUE, verbose = TRUE) {
# stopifnot(length(x) == 1, is.character(x))
file_exists <- file.exists(x)
if(strict) {
stopifnot(file_exists)
return(invisible(TRUE))
} else {
if(verbose) {
@tonyelhabr
tonyelhabr / contour.R
Last active October 10, 2019 12:49
R functions for creating a voronoi map for ERCOT.
download_spdf_bound_ercot <- function(..., path = config$path_spdf_bound, backup = FALSE) {
url <- 'http://www.ercot.com/content/cdr/static/ercot_boundary.kml'
download.file(url = url, destfile = config$path_kml_bound, quiet = TRUE)
bound <-
config$path_kml_bound %>%
xml2::read_xml() %>%
xml2::xml_children() %>%
xml2::xml_children() %>%
magrittr::extract(4) %>%
@tonyelhabr
tonyelhabr / utils-get.R
Last active February 14, 2020 00:37
R functions for retrieving data, possibly from local files.
import_andor_export <-
function(...,
data = NULL,
f = NULL,
.otherwise = NULL,
file = NULL,
ext = '.RData',
dir = 'output',
path = NULL,
var = NULL,
@tonyelhabr
tonyelhabr / schedule_task.R
Last active March 9, 2020 15:31
Schedule an R script using the {taskscheduleR} package (for Windows).
.get_config_schedule <-
function(path = NULL,
...,
dir = getwd(),
file = NULL,
index = 1,
taskname = NULL,
validate = FALSE) {
if(is.null(path)) {
path <- file.path(dir, file)
@tonyelhabr
tonyelhabr / timeit.R
Created June 16, 2020 12:37
Simple logic for pretty time-to-complete message
library(magrittr)
start <- Sys.time()
end <- Sys.time()
dur <- (end - start) %>% lubridate::as.duration()
dur_s <- round(as.numeric(dur, 'seconds'), 1) %>% scales::comma()
dur_m <- round(as.numeric(dur, 'minutes'), 1) %>% scales::comma()
cat(glue::glue('action took {dur_s} seconds ({dur_m} minutes) to complete.'))
@tonyelhabr
tonyelhabr / bin_touches_example.R
Last active April 9, 2021 12:08
Bin soccer event data.
library(tidyverse)
# general helper function
retrieve_sb_events <-
function(competition_id,
...,
clean = TRUE,
export = TRUE,
dir = 'data',
ext = 'rds',