Skip to content

Instantly share code, notes, and snippets.

View zross's full-sized avatar

Zev Ross zross

View GitHub Profile
@zross
zross / sf_dplyr.R
Created April 20, 2017 20:50
quick example of sf with dplyr producing spatial output
library(dplyr)
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc$blah <- sample(c(1,2,3), size = nrow(nc), replace = TRUE)
nc %>% group_by(blah) %>% summarize(avg_bir74 = mean(BIR74))
@zross
zross / readwrite_sf.R
Created March 30, 2017 12:58
Read and write from R to postgresql/postgis
library(sf)
library(RPostgreSQL)
dat <- data.frame(ID = 1:2,
long = c(-74.003234, -73.983317),
lat = c(40.731863, 40.722684))
dat_sf <- st_as_sf(dat, coords = c("long", "lat"),
crs = 4326,
agr = "identity")
con <- dbConnect(PostgreSQL(), dbname = "mydb", host = "localhost")
st_write_db(con, dat_sf, "mydata")
@zross
zross / data.js
Created March 24, 2017 20:34
Use highcharts directly
var mydat = [{"x":0.9229,"y":0.5069},{"x":-0.727,"y":0.0503},{"x":0.12,"y":0.7837},{"x":0.7549,"y":0.8554},{"x":0.3429,"y":0.5286},{"x":0.8708,"y":0.229},{"x":0.3001,"y":0.4375},{"x":1.2833,"y":0.3467},{"x":-0.0874,"y":0.2701},{"x":-1.712,"y":0.189},{"x":2.151,"y":0.0927},{"x":0.2291,"y":0.7824},{"x":2.0939,"y":0.2193},{"x":-1.3703,"y":0.0493},{"x":1.4011,"y":0.9804},{"x":-0.8699,"y":0.8298},{"x":0.1415,"y":0.2089},{"x":0.1937,"y":0.2329},{"x":1.3114,"y":0.6019},{"x":-2.0388,"y":0.6394},{"x":-0.9305,"y":0.8507},{"x":0.3093,"y":0.4594},{"x":-0.0593,"y":0.0129},{"x":-0.9854,"y":0.4071},{"x":0.4377,"y":0.4223},{"x":0.3431,"y":0.3752},{"x":-0.0743,"y":0.1136},{"x":1.4589,"y":0.2676},{"x":0.7697,"y":0.4361},{"x":0.0308,"y":0.6346},{"x":0.0329,"y":0.5116},{"x":-2.1853,"y":0.6104},{"x":1.5737,"y":0.5099},{"x":0.4319,"y":0.3776},{"x":-0.3889,"y":0.8447},{"x":-1.1621,"y":0.4509},{"x":0.3726,"y":0.8274},{"x":0.5568,"y":0.9947},{"x":1.1449,"y":0.9306},{"x":-0.3941,"y":0.834},{"x":-0.3685,"y":0.2573},{"x":-1.8967,"y":0.2
@zross
zross / fixFlash.R
Last active November 22, 2016 20:00
Flash redraw
# Joe Cheng provided this code
library(shiny)
#' Call `input <- createInputWrapper(input)` at the top of
#' your Shiny Server function.
createInputWrapper <- function(input) {
env <- new.env(parent = emptyenv())
env$impl <- input
env$blacklist <- new.env(parent = emptyenv())
# **********************************************************************************
# Data Science with R, Part 2 - Predictive modeling and machine learning: Exercise 1
# **********************************************************************************
# This exercise is designed to remind you of key R
# concepts and code that you will need to be familiar
# with before participating in Part 2 of the R workshop.
# Please feel free to use Google, stackoverflow etc to help
# you answer questions, treat them as if they were real-world
# challenges
@zross
zross / gist:6e998b69f8dd21cc74fd44218a9364af
Last active January 3, 2017 21:34
R installation for workshop

An Introduction to R for Data Science

During the workshop itself you only need a laptop and a browser. I will give you a URL and login information and you will run the software in the cloud. This way we're all working with the same software, same files and we don't have to use workshop time with software installation issues.

But, if you are adventurous and want to try installing the software locally on your machine before the workshop you can follow these instructions. You can e-mail me with questions beforehand (zev@zevross.com) and I can answer questions about your installation at the workshop as time permits.

Quick setup instructions

You will need to download and install the most recent version of R and the most recent version of RStudio. You will also need to install some R packages. Once you have R and RStudio installed you can install the packages by running this code in the console window:

@zross
zross / app.R
Last active March 4, 2016 21:36
library(shiny)
source("junk_inputwrapper.R")
server <- function(input, output, session) {
input <- createInputWrapper(input)
output$plot <- renderUI({
@zross
zross / east_town_mall.geojson
Created October 30, 2015 19:50
East Towne Mall
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@zross
zross / format_dates.md
Last active August 29, 2015 14:22
Formatting dates in R reference

R date formatting

#strptime() to convert from character to date/time
#format() to reformat the date

format(strptime("2001-05-05", "%Y-%m-%d"), "%A %B%e, %Y")
#"Saturday May 5, 2001"

format(strptime("2001-05-05 01:05 PM", "%Y-%m-%d %I:%M %p"), "%d/%m/%Y %H:%M")
@zross
zross / index.html
Created April 1, 2015 15:05
Simple Leaflet with CartoDB tiles
<!DOCTYPE html>
<html>
<head>
<title>Leaflet GeoJSON Example</title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<link href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js"></script>
<style>