Skip to content

Instantly share code, notes, and snippets.

View smach's full-sized avatar

Sharon Machlis smach

View GitHub Profile
generateTableCode <- function(mydata, headline="", caption="", sortable=TRUE, searchable=TRUE){
header_numstyle <- paste("<style>","div.table-wrapper table td.cwnumcol {","text-align: right;", "}", "</style>", sep="\n")
header_searchbox <- paste(
"<script type=\"text/javascript\">",
"$( document ).ready(function() {",
"var $rows = $('#cwsearchabletable tbody tr');",
"$('#cwtablesearchbox').keyup(function() {",
" var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();",
"$rows.show().filter(function() {",
" var text = $(this).text().replace(/\\s+/g, ' ').toLowerCase();",
library(readxl)
library(dplyr)
library(reshape2)
mydata <- read_excel("data/CaptainMelTournament2013.xls", sheet=1)
# Registration column is showing up, get rid of that. And note I removed the spaces from some column names to make them R friendly.
mydata <- mydata[,-1]
# reshape the data from wide to long
mydata_long <- melt(mydata, c("CheckinNumber", "FullName", "FirstName", "LastName", "Division"), variable.name = "Category", value.name = "Length")
# Example from R Graphics Cookbook by Winston Chang
# Install gcookbook package with
# install.packages("gcookbook")
# Bars have black outline while legend has outline + diagonal strike-through
library(gcookbook)
upc <- subset(uspopchange, rank(Change)>=40)
ggplot(upc, aes(x=reorder(Abb, Change), y=Change, fill=Region)) +
geom_bar(stat="identity", colour="black") +
@smach
smach / findwinner.R
Last active March 1, 2016 01:31
R function to find top vote-getter in a csv or Excel election results file. Requires rio package, and results file with candidate results in adjoining columns. findwinner function arguments: filename (string), datacolstart (number of data column where results data starts; 2 for column B in Excel, for example), datacolstop (number of the last dat…
function (filename, datacolstart, datacolstop, exportcsv = TRUE)
{
if (!require("rio"))
install.packages("rio")
data <- rio::import(filename)
for(i in 1:nrow(data)){
ranks <- rank(data[i,2:7])
maxrank <- as.numeric(max(ranks))
winners <- names(ranks[ranks==maxrank])
data$Winners[i] <- paste(winners, collapse = ", ")
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 5.
"Community","RegisteredVoters","Democrat","Republican","GreenRainbow","UnitedIndependent","Unenrolled","citytown","State","StateFIPS","PlaceFIPS","PlaceName","PlaceType","Status","County","Place","TotalHours","TotalEveningHours","SaturdayHours","SundayHours","TotalAMHours","NumLocations","ALAND_SQMI","VotersPerTotalHours","SqMilesPerLocation","VotersPerLocation","TotalNonBusiness"
"BOSTON",413670,217198,26198,607,2497,165823,"boston","MA",25,7000,"Boston city","County Subdivision","F","Suffolk County","Boston",101,10,6,0,0,28,48.366,4096,2,14774,16
"WORCESTER",104464,44656,8583,197,1076,49487,"worcester","MA",25,82000,"Worcester city","County Subdivision","F","Worcester County","Worcester",115.5,11,9,6,0,5,37.371,904,7,20893,26
"SPRINGFIELD",103923,53514,7957,122,626,41308,"springfield","MA",25,67000,"Springfield city","County Subdivision","F","Hampden County","Springfield",107,4,8,0,1,10,31.865,971,3,10392,13
"CAMBRIDGE",71351,40059,2760,222,283,27770,"cambridge","MA",25,11000,"Cambridge city","County Subdiv
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
mydata <- read.csv("https://gist.githubusercontent.com/smach/9cc166277488d2499e208d2076ff26c3/raw/e2f596ce6a134657e4daa73550510f6fb949890b/MAEarlyVotingData.csv", stringsAsFactors = FALSE)
mydata <- subset(mydata, RegisteredVoters >= 30000)
# This function and helper functions create a Leaflet interactive map in R from a World Bank indicator. This code was created by Kyle E. Walker, director of the Center for Urban Studies at Texas Christian University and (very) slightly modified by Sharon Machlis. See Walker's kwgeo package at https://github.com/walkerke/kwgeo/ for more info. Any errors are most certainly Sharon's.
if (!require("WDI")) install.packages("WDI")
if (!require("rgdal")) install.packages("rgdal")
if (!require("sp")) install.packages("sp")
if (!require("leaflet")) install.packages("leaflet")
if (!require("httr")) install.packages("httr")
if (!require("maptools")) install.packages("maptools")

Keybase proof

I hereby claim:

  • I am smach on github.
  • I am sharon000 (https://keybase.io/sharon000) on keybase.
  • I have a public key whose fingerprint is C87F C261 72AE 1D42 1CCD 1F23 2C3F B45F 022F 6E5A

To claim this, I am signing this object:

Winter Boston Chicago NYC
1940-1941 47.8 52.5 39
1941-1942 23.9 29.8 11.3
1942-1943 45.7 45.2 29.5
1943-1944 27.7 24 23.8
1944-1945 59.2 34.9 27.1
1945-1946 50.8 23.9 31.4
1946-1947 19.4 34.1 30.6
1947-1948 89.2 38.1 63.2
1948-1949 37.1 14.3 46.6
@smach
smach / google_calendar_demo.R
Last active October 2, 2017 23:34 — forked from MarkEdmondson1234/google_calendar_demo.R
A demo of calling Google Calendar API
library(googleAuthR)
## set scopes for calendar
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/calendar.readonly",
googleAuthR.client_id = "XXXX", ## add your Google project client Id - find it at https://console.developers.google.com/apis/credentials then click on the appropriate OAuth 2.0 client ID
googleAuthR.client_secret = "XXXX") ## add your Google project client secret - at same place as above
## make sure calendar API is activated for your Google Project at below URL:
# https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/overview