Skip to content

Instantly share code, notes, and snippets.

View keberwein's full-sized avatar

Kris Eberwein keberwein

View GitHub Profile
@keberwein
keberwein / server.R
Created September 23, 2015 15:11 — forked from withr/server.R
Encrypt password with md5 for Shiny-app.
library(shiny)
library(datasets)
Logged = FALSE;
PASSWORD <- data.frame(Brukernavn = "withr", Passord = "25d55ad283aa400af464c76d713c07ad")
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
source("www/Login.R", local = TRUE)
observe({
if (USER$Logged == TRUE) {
@keberwein
keberwein / countyfips_map.R
Last active October 28, 2016 15:25
An example of matching FIPS codes to shape files to map data in R.
library(sp)
library(ggplot2)
library(rgeos)
library(rgdal)
library(maptools)
library(tigris)
### Begin data prep
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
@keberwein
keberwein / countystr_map.R
Created June 15, 2016 18:13
Create a map of US counties by string matching county and state name.
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
dat <- read.csv(url, stringsAsFactors = FALSE)
# Colnames tolower
names(dat) <- tolower(names(dat))
dat$countyname <- tolower(dat$countyname)
# Wide data set, subset only what we need.
county_dat <- subset(dat, measureid == "296",
select = c("countyfips","statename", "countyname", "value", "unitname"))
# Rename columns to make for a clean df merge later.
@keberwein
keberwein / countyfips_leaflet.R
Last active February 23, 2023 01:34
A leaflet map for R built on a shape file of US counties
library(sp)
library(rgeos)
library(rgdal)
library(maptools)
library(dplyr)
library(leaflet)
library(scales)
### Begin data prep
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
@keberwein
keberwein / storm_trackr.R
Last active October 4, 2016 15:11
Track Unisys data available tropical storms
library(plyr); library(dplyr); library(leaflet); library(stringi);
library(htmltools); library(RColorBrewer); library(rvest)
# Parse and read storm track data.
html <- read_html('http://weather.unisys.com/hurricane/atlantic/2016/index.php')
links <- html_attr(html_nodes(html, "a"), "href")
links <- links[grep('track.dat', links)]
track <- select.list(links, title="Select storm:", graphics = FALSE)
#track <- "MATTHEW/track.dat"
@keberwein
keberwein / restart_nginx.bat
Created September 2, 2016 16:28
Restart Nginx Windows
@ECHO OFF
cd /nginx
taskkill /f /IM nginx.exe
start nginx
EXIT
@keberwein
keberwein / countyMap_ggplot.R
Last active October 28, 2016 15:43
Map County Unemployment with ggplot2
library(blscrapeR)
library(ggplot2)
library(dplyr)
# Get the current county data from blscrapeR.
df <- get_bls_county(stateName = "Ohio")
# Grap the internal FIPS codes data set from blscrapeR so we can match on counties.
fips <- blscrapeR::county_fips
# Subset fips list to only Ohio counties.
fips <- subset(fips, state=="OH")
@keberwein
keberwein / projections.R
Last active July 4, 2021 20:38
Map projections for the USA
# Makes sure the required packs are loaed, if not, intalles them.
pkgs <-c ('sp','ggplot2','rgdal','broom','maptools','tigris', 'blscrapeR')
for(p in pkgs) if(p %in% rownames(installed.packages()) == FALSE) {install.packages(p, repos='http://cran.us.r-project.org')}
for(p in pkgs) suppressPackageStartupMessages(library(p, quietly=TRUE, character.only=TRUE))
# Before we get into the projections, let's download some unemployment data to map.
county_dat <- get_bls_county()
# We also need a shapefile with the appropriate FIPS codes.
state <- counties(cb = TRUE, year = 2015)
@keberwein
keberwein / woba.R
Created December 6, 2016 18:48
An R adaptation of Tom Tango’s calculation for wOBA using the Lahman database.
library(Lahman); library(dplyr)
# Find primary positions
fielding <- Lahman::Fielding
# The "postf" field below is to filter out Natl. League players who may have
# played as DH in inter-leauge games, and may have multiple entries at diff. positions.
PrimPos <- dplyr::mutate(fielding, postf=ifelse(POS=="OF" & yearID>1995, 1,0)) %>%
subset(postf==0, select=c("playerID", "yearID", "teamID", "lgID","G", "POS")) %>%
group_by(playerID, yearID, teamID, lgID, POS) %>%
summarise(G = sum(G))
@keberwein
keberwein / woba.sql
Created December 6, 2016 18:50
A MySQL adaptation of Tom Tango’s calculation for wOBA using the Lahman database.
CREATE TABLE PrimPos AS
SELECT playerID
, yearID
, teamID
, MAX(G) AS G
, POS
FROM (SELECT * from fielding
WHERE IF(yearID>1995 AND POS = "OF",1,0) != 1 ORDER BY G Desc) f
GROUP BY playerID, yearID, teamID, POS;