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 / useful-ensurers.R
Created April 9, 2016 18:30
Basic insurance against bad args to .Fortran() calls
eval({
ensure_nonnull <- ensures_that(!is.null(.) ~ "Must not be NULL" )
ensure_nonempty <- ensures_that(length(.) > 0 ~ "Must not be empty", +ensure_nonnull )
ensure_finite <- ensures_that(all(is.finite(.)) ~ "Must be finite", +ensure_nonempty )
ensure_positive <- ensures_that(all(. > 0) ~ "Must be positive", +ensure_finite )
ensure_nonnegative <- ensures_that(all(. >= 0) ~ "Must be positive or zero", +ensure_finite )
ensure_unique <- ensures_that(length(unique(.)) == 1)
})
# Wrapped in `eval({...})` b/c otherwise RStudio will complain about syntax
@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 / 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 / hbase-env.sh
Last active December 26, 2015 13:59
HBase 0.94.11 on OS X Mavericks
#
#/**
# * Copyright 2007 The Apache Software Foundation
# *
# * Licensed to the Apache Software Foundation (ASF) under one
# * or more contributor license agreements. See the NOTICE file
# * distributed with this work for additional information
# * regarding copyright ownership. The ASF licenses this file
# * to you under the Apache License, Version 2.0 (the
# * "License"); you may not use this file except in compliance
@dholstius
dholstius / core-site.xml
Last active December 26, 2015 13:59
Hadoop 1.2.1 on OS X Mavericks
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:9000</value>
</property>
@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 / format.r
Created November 27, 2012 21:12
Metric and significant-digit formatters for use with ggplot2
#' format_signif
#'
#' Format numbers with a given number of significant digits
#'
#' @param x numeric vector
#' @param digits number of significant digits
#' @return character vector
#' @examples
#' x <- c(0.800, 1704.1, 43.5e6, 10, NaN, NA, Inf, 0.00)
#' format_signif(x, digits=3)
@dholstius
dholstius / scale_x_hours.r
Created November 27, 2012 20:46
Helpful scales for use with ggplot2
require(ggplot2)
require(scales)
seq_range <- function(x, by) seq(min(x), max(x), by=by)
time_breaks <- function(width) {
function(limits) seq_range(limits, by=width)
}
scale_x_hours <- function(
@dholstius
dholstius / fast_POSIXct.r
Created November 27, 2012 20:12
Quickly convert local timestamps to a POSIXct vector
#' fast_POSIXct
#'
#' Quickly convert local timestamps to a POSIXct vector
#'
#' @param x timestamps (YYYY-mm-dd HH:MM:SS)
#' @param tz local timezone
#' @return POSIXct vector
#' @export
fast_POSIXct <- function(x, tz) {
stopifnot(is.character(x))
@dholstius
dholstius / aggregate.data.table.r
Last active October 13, 2015 06:57
Fast rollup of rows from a data.frame (uses data.table for speed)
#' aggregate.data.table
#'
#' Use to quickly aggregate rows from a data.frame
#'
#' @param x data.table
#' @param by list of columns to use for grouping
#' @param FUN summary function
#' @param \dots further arguments to summary function
#' @param is.value determines which of the remaining columns should be aggregated (numeric, POSIXct, character, etc.)
#' @author David Holstius \email{david.holstius@berkeley.edu}