Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
library(shiny)
runApp(list(
ui = fluidPage(downloadButton('foo')),
server = function(input, output) {
plotInput = reactive({
plot(1:10)
})
output$foo = downloadHandler(
filename = 'test.pdf',
content = function(file) {
@geotheory
geotheory / server.R
Last active August 29, 2015 14:11
Output (non-ggplot) Shiny plots to PDF
library(shiny)
shinyServer(
function(input, output) {
plotInput <- reactive({
if(input$returnpdf){
pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h))
plot(rnorm(sample(100:1000,1)))
dev.off()
}
@geotheory
geotheory / server.R
Last active August 29, 2015 14:11
R Shiny stability testing
require(shiny)
# inbuilt dataset
diamonds = ggplot2::diamonds[,c(1,5,7)]
# csv datasets to input via front-end
for(i in 1:3){
dat <- diamonds[sample(1:nrow(diamonds), 200),]
write.table(dat, paste0('dat',i,'.csv'), sep=',',row.names=F, col.names=T)
}
<?php
/**
* Website: http://sourceforge.net/projects/simplehtmldom/
* Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)
* Contributions by:
* Yousuke Kumakura (Attribute filters)
* Vadim Voituk (Negative indexes supports of "find" method)
* Antcs (Constructor with automatically load contents either text or file/url)
*
* all affected sections have comments starting with "PaperG"
@geotheory
geotheory / gist:e1275221680c668485b0
Created January 1, 2015 23:23
PHP Simple HTML DOM Parser - comma test
<!DOCTYPE html>
<html>
<head>
<title>Comma-URL test</title>
</head>
<body>
<h1>Comma-URL test</h1>
<p>Let's try and parse: http://en.m.wikipedia.org/wiki/National_Register_of_Historic_Places_listings_in_San_Francisco,_California</p>
<?php
@geotheory
geotheory / stacked_geom_bar.R
Last active August 29, 2015 14:13
ggplot geom_bar: stack and center
require(ggplot2); require(tidyr); require(dplyr)
dat = data.frame(grp=letters[1:3], very_bad=c(4,2,1), bad=c(3,4,2), good=c(2,3,4), very_good=c(1,1,3))
(d = dat %>% tidyr::gather(rating, value, very_bad:very_good) %>%
mutate(rating = ordered(rating, levels=c('very_bad','bad','good','very_good'))))
ggplot() +
geom_bar(data = subset(d, rating %in% c('very_bad','bad')),aes(grp, -value, fill=rating), position="stack", stat="identity") +
geom_bar(data = subset(d, !rating %in% c('very_bad','bad')), aes(grp, value, fill=rating), position="stack", stat="identity") +
scale_fill_manual(values=colorRampPalette(c('red','grey','green'))(4)) +
@geotheory
geotheory / rwantshue_example.R
Last active August 29, 2015 14:13
Quick rwantshue demo
# *** PACKAGE IS CURRENTLY UNDER DEVELOPMENT SO THIS CODE MAY NOT WORK ***
# this doesn't do the iwanthue tool justice it just shows the flow
# checkout http://tools.medialab.sciences-po.fr/iwanthue/ for detail
require(devtools)
install_github("hoesler/rwantshue")
require(rwantshue)
scheme <- iwanthue()
@geotheory
geotheory / bbc_weather.csv
Last active February 23, 2016 15:28
A year of BBC's Weather API data for select cities
We can't make this file beautiful and searchable because it's too large.
date,day,city,desc,maxtemp,mintemp,wind_dir,wind_speed,visibility,pressure,humidity,uv_risk,pollution
28/06/2014,0,London,Heavy Rain,20,12,East South Easterly,6mph,Poor,1010mb,66%,5,Low
28/06/2014,1,London,Sunny Intervals,20,12,North North Westerly,10mph,Good,1013mb,45%,6,Low
28/06/2014,2,London,Sunny Intervals,21,13,South Easterly,5mph,Very Good,1018mb,45%,6,Low
28/06/2014,0,Manchester,Light Rain,16,11,North Easterly,6mph,Moderate,1012mb,82%,3,Low
28/06/2014,1,Manchester,Sunny Intervals,18,10,North North Westerly,7mph,Very Good,1014mb,48%,6,Low
28/06/2014,2,Manchester,Sunny Intervals,19,12,South South Westerly,3mph,Very Good,1017mb,52%,6,Low
28/06/2014,0,Cardiff,Thundery Shower,18,13,North North Easterly,3mph,Very Good,1009mb,64%,5,Low
28/06/2014,1,Cardiff,Light Rain,18,11,South Westerly,6mph,Good,1014mb,67%,6,Low
28/06/2014,2,Cardiff,Light Cloud,19,13,South South Easterly,7mph,Very Good,1016mb,57%,6,Low
@geotheory
geotheory / HOTOSM_Bounding_Box.js
Last active February 23, 2016 15:26
HOTOSM Project Bounding Box
// When using HOTOSM (e.g. http://tasks.hotosm.org/project/1513), run this
// in the browser's console and the project bounding box will be returned
(function (){
var xmin = 360; var xmax = 0; var ymin = 90; var ymax = -90;
for(var i=0; i<geometry.coordinates.length; i++){
var set = geometry.coordinates[i][0];
for(var j=0; j<set.length; j++){
var x = set[j][0];
var y = set[j][1];
if(x > xmax) xmax = x;
@geotheory
geotheory / map_aspect.R
Created July 31, 2015 13:07
Get aspect ratio for a small-area lat-long dataset to approximate Mercator projection
# ref: http://stackoverflow.com/questions/31745894/get-aspect-ratio-for-lat-long-plots/31746542#31746542
# requires ggplot2
map_aspect = function(x, y) {
x.center <- sum(range(x)) / 2
y.center <- sum(range(y)) / 2
x.dist <- ggplot2:::dist_central_angle(x.center + c(-0.5, 0.5), rep(y.center, 2))
y.dist <- ggplot2:::dist_central_angle(rep(x.center, 2), y.center + c(-0.5, 0.5))
y.dist / x.dist
}