Skip to content

Instantly share code, notes, and snippets.

View dholstius's full-sized avatar

David Holstius dholstius

  • Bay Area Air Quality Management District
  • San Francisco Bay Area
  • X @dholstius
View GitHub Profile
@dholstius
dholstius / patch.R
Last active May 10, 2019 21:52
Patch data on-the-fly (DRAFT)
#' Patch data on the fly.
#'
#' @param object to be patched
#' @param cond logical condition(s) to be evaluated within scope of object
#' @param \dots name-value pairs
#' @param quiet suppress messages
#'
#' @examples
#' patch(mtcars, where(vs == 0, am == 1), gear = Inf, carb = carb + 10)
#'
@dholstius
dholstius / ensurer-example-data_frame.R
Last active August 29, 2015 14:26
Using `ensurer` to QA elements of a `data.frame`
library(ensurer)
ensure_none_missing <- ensures_that(!any(is.na(.)) ~ "There are missing values")
ensure_all_positive <- ensures_that(all(int(.)) > 0 ~ "Not all values are positive", +ensure_none_missing)
# Everything works as expected with a simple vector
x <- 1:4
x %>% ensure_all_positive
# Now let's try testing element(s) of a data.frame
@dholstius
dholstius / curry_substr.R
Last active August 29, 2015 14:25
Unexpected result of currying substr()
extract_substrings <- function (x, ...) {
make_extractor <- function (i) {
#message(min(i), " to ", max(i))
function (s) substr(s, min(i), max(i))
}
extractors <- lapply(list(...), make_extractor)
lapply(extractors, function (f) f(x))
}
ALPHABET <- paste0(LETTERS, collapse = "")
@dholstius
dholstius / Untitled.Rmd
Created April 16, 2015 16:07
Reactive context for leaflet() in dynamic Rmarkdown document (runtime:shiny)
---
title: "Reactive Leaflet"
author: "David Holstius"
date: "April 16, 2015"
output: html_document
runtime: shiny
---
## Problem Description
@dholstius
dholstius / sp-merge-example.R
Last active August 29, 2015 14:17
Merging polygons without dissolving adjacent boundaries
# See http://stackoverflow.com/questions/29310962/variant-of-rgeosgunion-that-wont-dissolve-adjacent-polygons
# for the relevant problem formulation.
library(sp)
unit_rect <- cbind(
x = c(0, 0, 1, 1, 0),
y = c(0, 1, 1, 0, 0))
translate <- function (spobj, delta = c(0, 0)) {
@dholstius
dholstius / readWKT-by_row.R
Last active August 29, 2015 14:17
Row-wise application of rgeos::readWKT
library(rgeos)
library(dplyr)
library(magrittr)
n_polygons <- 5
n_vertices_each <- 3
rand_coords <- function (n)
sprintf("%0.1f %0.1f", rnorm(n), rnorm(n))
@dholstius
dholstius / searchable.R
Created March 18, 2015 19:12
Searchable columns using DT::datatable()
mtcars %>%
datatable(options = list(columnDefs = list(list(searchable = TRUE, targets = 1:3))))
@dholstius
dholstius / keybase.md
Created February 27, 2015 23:45
keybase.md

Keybase proof

I hereby claim:

  • I am holstius on github.
  • I am dholstius (https://keybase.io/dholstius) on keybase.
  • I have a public key whose fingerprint is 6974 E4D6 0075 DA92 FE5B CA28 D2E8 C03F DDAB A245

To claim this, I am signing this object:

@dholstius
dholstius / cached.R
Last active August 29, 2015 14:14
file-based caching for R
abs_path <- function (...) {
normalizePath(file.path(...), mustWork = FALSE)
}
cache_put <- function (.expr, path, ...) {
obj <- eval(.expr)
dn <- dirname(path)
if (!file.exists(dn)) {
dir.create(dn, recursive = TRUE)
}
@dholstius
dholstius / stacked_area_chart.R
Last active August 29, 2015 14:13
Stacked area chart in ggivs
library(dplyr)
library(ggivs)
chart_data <- structure(list(CalYr = c(1990L, 1990L, 1990L, 1990L, 1990L, 1990L,
1990L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L), Speed = c(0, 10, 20, 30, 40, 50,
60, 0, 10, 20, 30, 40, 50, 60, 0, 10, 20, 30, 40, 50, 60, 0,
10, 20, 30, 40, 50, 60), VMT = c(323694, 940682, 25396442, 43050277,
71101181, 16987641, 105467584, 222397, 859767, 57905435, 61835130,