Skip to content

Instantly share code, notes, and snippets.

View jsta's full-sized avatar
🐧
...

Jemma Stachelek jsta

🐧
...
View GitHub Profile
@jsta
jsta / tidestats.r
Last active August 29, 2015 14:02
calculate tidal statistics
library(Tides)
#generate date time sequence for 6 sec frequency series
dt<-read.table("clipboard",header=FALSE,colClasses=c("character","character","numeric","numeric"),col.names=c("Date","Time","wlev","stage"))
dt$DateTime<-as.POSIXct(paste(dt$Date,dt$Time),format="%Y%j %H%M")
dt2<-data.frame(dt$DateTime,dt$stage)
names(dt2)<-c("time","h")
dt2<-dt2[complete.cases(dt2),]
TidalCharacteristics(dt2,0.4,dtMax=1680)
@jsta
jsta / server.R
Last active August 29, 2015 14:17 — forked from hrbrmstr/server.R
shiny gis
library(shiny)
library(maps)
library(maptools)
library(ggplot2)
library(plyr)
library(XML)
# Define server logic required to plot various variables against mpg
shinyServer(function(input, output, session) {

Lesson 00 - Setup + R Intro

Start Rstudio Create Project Create a Data Directory and Rscript

Explain:

  • diff btw console and script
  • + sign means waiting for more input
1 + 1
# assign b to 2
b <- 2
a <- 3
b <- a
a <- 6
# exploring sqrt function
b <- 4
# work-through of https://github.com/USGS-R/mda.lakes/issues/91
set.seed(443)
trend = 0.04 #in units per year, kinda like wtemp
freq = 26 #sample freq
year = (1:(30*freq)/freq)
yobs = 10*sin(year*2*pi)+ 10 + trend*year + rnorm(length(year), sd=0)
ymod = 10*sin(year*2*pi) + 10 + (trend*max(year)/2)
@jsta
jsta / level-pool-routing.R
Last active November 23, 2016 19:28
Reservoir level pool routing
lagpad <- function(x, k) {
c(rep(NA, k), x)[1 : length(x)]
}
level_pool_routing <- function(lt, qh, area, delta_t, initial_outflow, initial_storage, linear.fit){
lt$ii <- apply(cbind(lagpad(lt$inflow, 1), lt$inflow), 1, sum)
if(is.null(qh$storage)){
qh$storage <- area * qh$elevation
@jsta
jsta / scrape_wiki_lakes.R
Last active January 7, 2017 16:46
Scrape lake metadata tables from Wikipedia
library(WikipediR)
library(rvest)
get_lake_wiki <- function(lake_name){
res <- WikipediR::page_content("en", "wikipedia", page_name = lake_name,
as_wikitext = FALSE)
res <- res$parse$text[[1]]
res <- xml2::read_html(res)
res <- rvest::html_nodes(res, "table")
@jsta
jsta / plos_supp.R
Last active December 2, 2016 17:21
Get PLOS supplementary material with R
# http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0081457
library(fulltext)
library(alm)
query <- ft_search(query = "Estimating Summer Nutrient Concentrations in
Northeastern Lakes from SPARROW Load Predictions and Modeled
Lake Depth and Volume", from = "plos")
doi <- ft_links(query)$plos$ids
alm_ids(doi = doi, info = "detail")$data$info$title
@jsta
jsta / 100-numbers_alpha-order.R
Last active December 25, 2016 04:12
The first one hundred natural numbers in alphabetical order
# https://www.reddit.com/r/mildlyinteresting/comments/5k4eqd/the_first_100_positive_integers_in_alphabetical
# https://imgur.com/Ylmw8K6
source("https://raw.githubusercontent.com/ateucher/useful_code/master/R/numbers2words.r")
dt <- numbers2words(1:100)
dt <- order(dt)
dt <- matrix(dt, nrow = 10, byrow = TRUE)
knitr::kable(data.frame(dt), caption = "The first one hundred natural numbers in alphabetical order")
@jsta
jsta / handbrakecliR
Created January 4, 2017 14:25
R script to interact with the handbrakeCLI program
# need to apt-get:
# * libdvdcsss2
# * handbrake-cli
handbrake_scan <- function(source){
cli <- paste0("HandBrakeCLI -i ", source, " --scan")
system(cli)
}
handbrake_scan("/media/user/disc")