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 / 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 / pachube-getDatapoints.R
Created June 6, 2012 23:37
How to fetch data points with the pachube R package
require(pachube)
key <- 'bZW0wu4jbqMrkzHhapw8E9...' # use your API key here
t0 <- ISOdate(2012, 05, 23, hour=19, tz="America/Los_Angeles")
z <- getDatapoints(58785, 'large', key, start=t0, interval=30, duration='12hours')
plot(z)
key <- 'bZW0wu4jbqMrkzHhapw8E9...' # use your API key here
t0 <- ISOdate(2012, 05, 23, hour=19, tz="America/Los_Angeles")
z <- getDatapoints(58785, 'large', key, start=t0, interval=30, duration='12hours')
@dholstius
dholstius / gist:2885515
Created June 6, 2012 23:36
How to fetch datapoints with the pachube R package
key <- 'bZW0wu4jbqMrkzHhapw8E9...' # use your API key here
t0 <- ISOdate(2012, 05, 23, hour=19, tz="America/Los_Angeles")
z <- getDatapoints(58785, 'large', key, start=t0, interval=30, duration='12hours')
plot(z)
@dholstius
dholstius / R-pachube-getDatapoints
Created June 6, 2012 23:37
How to fetch datapoints with the pachube R package
key <- 'bZW0wu4jbqMrkzHhapw8E9...' # use your API key here
t0 <- ISOdate(2012, 05, 23, hour=19, tz="America/Los_Angeles")
z <- getDatapoints(58785, 'large', key, start=t0, interval=30, duration='12hours')
plot(z)
@dholstius
dholstius / feed_history.R
Created June 6, 2012 23:43
Installing devtools and then cosm
require(cosm)
key <- 'bZW0wu4jbqMrkzHhap...' # your API key here
z <- feed_history(58785, key, duration='3hours')
plot(z)
@dholstius
dholstius / download-bfs.R
Created June 21, 2012 00:44
Extended historical queries with the cosm R package
require('cosm')
if (!exists('key')) key <- readline('API key: ')
feed <- getFeed(58537, key)
print(feed$description)
t0 <- Sys.time()
duration <- 60 * 60 * 24 * 7 # one week
z <- getDatapoints(feed, key, datastreams='2h', end=t0, start=t0-duration)
@dholstius
dholstius / read.serlog.R
Created June 22, 2012 18:39
Read CSV records from a file written by serlog.py
#' read.serlog
#'
#' Read CSV records from a file written by serlog.py
#'
#' @param file filename
#' @param varnames column names for the resulting \link{zoo} object
#' @export
read.serlog <- function(file, varnames) {
require(zoo)
options(digits.secs=3)