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 / 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 / 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 / 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 / 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 / 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 / fun_join.R
Last active December 23, 2015 05:41
(Left) join with custom comparator
#' (Left) join with a custom comparator
#'
#' @param left data.frame
#' @param right data.frame
#' @param by names of columns to join by
#' @param fun custom comparator (see examples)
#'
#' @examples
#' my_df <- data.frame(cyl = c(4, 4, 6, 8), vs = c(0, 1, NA, NA), foo = c("A", "B", "C", "D"))
#' my_fun <- function (e1, e2) (e1 == e2) | is.na(e2)
@dholstius
dholstius / nginx.conf
Last active January 28, 2016 17:55
nginx conf for rstudio and shiny (/etc/nginx/sites-enabled/)
# via https://support.rstudio.com/hc/en-us/articles/200552326-Running-RStudio-Server-with-a-Proxy
user nginx;
worker_processes 4;
# daemon off;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
@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 / filter_-sp-S3-methods.R
Last active March 22, 2016 12:46
filter_() methods for Spatial*DataFrame objects
#' Filter (subset) a Spatial*DataFrame object
#'
#' @param object a \code{Spatial*DataFrame}
#' @param ... see \link{subset}
#' @param .dots
#'
#' @importFrom lazyeval all_dots lazy_eval
#'
#' @return a subset of the original \code{Spatial*DataFrame}
#'
@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 }