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 / CES_data-examples.R
Last active August 29, 2015 14:07
Export tract-level CalEnviroScreen 2.0 estimates for PM25 and DieselPM to shapefile
# Installation
library(devtools)
install_github("BAAQMD/CalEnviroScreen")
# Helper function: merge.SpatialPolygonsDataFrame()
source_gist("8d1aa85a1623243f8f1f")
# Data from CalEnviroScreen 2 (CES2) package
data(California, package = "CalEnviroScreen")
data(CES2, package = "CalEnviroScreen")
@dholstius
dholstius / merge.SpatialPointsDataFrame.R
Created October 2, 2014 18:58
Merge a SpatialPolygonsDataFrame with a data.frame
merge.SpatialPolygonsDataFrame <- function (x, y, by, ...) {
require(sp)
y <- as.data.frame(y)
if (missing(by)) {
if (is.null(row.names(x@data)) || is.null(row.names(y))) {
warning("[merge.SpatialPolygonsDataFrame] merging by position")
i <- 1:nrow(x@data)
} else {
warning("[merge.SpatialPolygonsDataFrame] merging by row names")
i <- row.names(x@data)
@dholstius
dholstius / read_tbl.R
Last active August 29, 2015 14:08
read_tbl
read_tbl <- function (..., stringsAsFactors = FALSE, check.names = FALSE) {
require(dplyr)
as.tbl(read.csv(..., stringsAsFactors = stringsAsFactors, check.names = check.names))
}
write_tbl <- function (x, ..., row.names = FALSE) {
write.csv(as.tbl(x), ..., row.names = row.names)
}
@dholstius
dholstius / .gitignore
Last active August 29, 2015 14:09 — forked from mbostock/.block
California census tracts
.DS_Store
build
node_modules
@dholstius
dholstius / titlecase.R
Created November 18, 2014 23:54
titlecase.R
titlecase <- function (x) {
# TODO: use Rex package
gsub("\\b([a-z])([a-z]+)", "\\U\\1\\L\\2", tolower(x), perl = 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,
@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 / 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 / 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 / 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))