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 / rgdal-install-with_homebrew.R
Last active April 13, 2018 19:44
rgdal installation on OS X
install_from_source <- function (pkg, args = NULL, repos = "http://cran.rstudio.com", overwrite = FALSE, ...) {
if (!overwrite) {
if (pkg %in% installed.packages()) {
message(pkg, " is already installed")
return()
}
}
install.packages(
@dholstius
dholstius / BAAQMD-CCl4_emissions-PY_1997_2016.R
Last active November 10, 2017 17:10
Carbon Tetrachloride (CCl4) Emissions, 1997-2016
PY_CCl4_emission_data <-
PY(1997:2016) %>%
point_source_abated_emissions() %>%
filter_pollutants(list(CCl4 = 60))
chart_data <-
PY_CCl4_emission_data %>%
mutate(fac_h1 = if_else(fac_id == 31, "Dow Chemical", "All Others")) %>%
annual_emissions_by(fac_h1) %>%
group_by(fac_h1)
@dholstius
dholstius / UScensus2010-blk_data-reprex.R
Created August 25, 2017 16:09
Reprex for UScensus2010 block level demographics
library(UScensus2010)
library(UScensus2010tract)
library(UScensus2010blkgrp)
library(UScensus2010blk)
Cook_tracts <- UScensus2010::county(state = "Illinois", name = "Cook", level = "tract")
Cook_tracts@data[1:8, 1:8]
plot(Cook_tracts)
Cook_blkgrps <- UScensus2010::county(state = "Illinois", name = "Cook", level = "blkgrp")
@dholstius
dholstius / stat_rollapplyr.R
Created June 8, 2012 22:50
Apply a rolling mean (or other function) to a ggplot2 layer
require(ggplot2)
require(proto)
StatRollApplyR <- proto(ggplot2:::Stat, {
required_aes <- c("x", "y")
default_geom <- function(.) GeomLine
objname <- "rollapplyr"
calculate_groups <- function(., data, scales, ...) {
.super$calculate_groups(., data, scales, ...)
}
calculate <- function(., data, scales, width, FUN, fill=NA, ...) {
@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 / rpivotTable-mySum.R
Created September 25, 2016 21:35
Try to replicate Sum aggregator
library(dplyr)
library(htmlwidgets)
library(rpivotTable)
mtcars %>%
rpivotTable(rows = "gear",
cols = c("cyl", "carb"),
vals = "hp",
# Step 1. Make this line accomplish the same thing as the default
@dholstius
dholstius / grouply.R
Last active August 31, 2016 17:11 — forked from dgrtwo/grouply.R
grouply <- function(f, ...) {
groups <- lazyeval::lazy_dots(...)
function(tbl, ...) {
dplyr::group_by_(tbl, .dots = groups) %>%
f(...) %>%
dplyr::ungroup()
}
}
@dholstius
dholstius / install-libraries.R
Last active May 18, 2016 18:18
R 3.3 library
# $ brew install libssh2
# $ brew install libgit2
## $ export CMAKE_INCLUDE_PATH="/usr/local/Cellar/libssh2/1.7.0/include"
## $ export CMAKE_LIBRARY_PATH="/usr/local/Cellar/libssh2/1.7.0/lib"
# NOTE: re: git2r: https://github.com/ropensci/git2r/issues/236
install_CRAN <- function (pkg, repos = "http://cran.rstudio.com", verbose = TRUE, ...) {
if (pkg %in% installed.packages()) message(pkg, " is already installed")
else install.packages(pkg, repos = repos, verbose = verbose, ...)
}
@dholstius
dholstius / ensurer-fail_with-experiments.R
Last active April 22, 2016 17:13
Using the dot (`.`) in `fail_with`
#
# Context:
#
# https://cran.r-project.org/web/packages/ensurer/vignettes/ensurer.html says:
# "It is also possible to use the dot, ., directly in anonymous error handlers defined directly in the call to e.g. ensure_that."
#
# I must be misunderstanding the statement above!
#
library(ensurer)
@dholstius
dholstius / check-groceries.R
Last active April 11, 2016 15:55
Adventures with `ensurer`
library(ensurer) # awesome
# Example 1 --- OK
groceries <- c("eggs", "bread", "milk")
check_that(groceries, "bread" %in% .)
# Example 2 --- :-(
# I'd really like this to work! It's quite readable.
# Doesn't work b/c `ensurer` (v1.1) doesn't first evaluate calls.
contains <- function (what) function (x) { what %in% x }