Skip to content

Instantly share code, notes, and snippets.

View markheckmann's full-sized avatar
🐕

Mark Heckmann markheckmann

🐕
View GitHub Profile
library(sp)
library(RColorBrewer)
# get spatial data for Germany on county level
con <- url("http://gadm.org/data/rda/DEU_adm3.RData")
print(load(con))
close(con)
# plot Germany with random colors
col = rainbow(length(levels(gadm$NAME_3)))
### DATA PREP ###
# loading the unemployment data
unempl <- read.delim2(file="./data/data_germany_unemployment_by_
county.txt", header = TRUE, sep = "\t",
dec=",", stringsAsFactors=F)
# due to Mac OS encoding, otherwise not needed
gadm_names <- iconv(gadm$NAME_3, "ISO_8859-2", "UTF-8")
# fuzzy matching of data: quick & dirty
@markheckmann
markheckmann / server.R
Created November 19, 2013 23:23
client/server interaction with shiny - part 1
library(shiny)
shinyServer( function(input, output, session) {
output$results <- renderPrint({
input$mydata
})
})
@markheckmann
markheckmann / server.R
Created November 19, 2013 23:26
client/server interaction with shiny - part 2
library(shiny)
shinyServer( function(input, output, session) {
output$results <- renderPrint({
input$mydata
})
# observer if value of the data sent from the client changes
# if yes generate a new random color and send it back to
@markheckmann
markheckmann / mycode.js
Created November 20, 2013 13:35
client/server interaction with shiny - part 3 (separate JS file)
// Execute function body when the HTML document is ready
$(document).ready(function() {
// javascript code to send data to shiny server
document.getElementById("mydiv").onclick = function() {
var number = Math.random();
Shiny.onInputChange("mydata", number);
};
@markheckmann
markheckmann / server.R
Created December 31, 2013 01:44
shiny progress bar
library(shiny)
library(shinyIncubator)
shinyServer(function(input, output, session) {
output$out2 <- renderPrint({
"output 2"
})
output$plot <- renderPlot({
shinyServer(function(input, output, session) {
observe({
input$btn
session$sendCustomMessage(type = "resetFileInputHandler", "file1")
})
})
@markheckmann
markheckmann / postprocess_markdown.R
Last active August 29, 2015 14:11
Post-process markdown code to paste into Wordpress
change_tex_delimiters <- function(x)
{
# replace latex equation delimiters ($$) if present
eq <- which(x == "$$") # find positions of equations
if (length(eq) > 0) {
eq.begin <- eq[c(T,F)]
eq.end <- eq[c(F,T)]
for (i in eq.begin)
x[[i]] <- "$latex \\displaystyle"
for (i in eq.end)
@markheckmann
markheckmann / hcl_color_wheels
Last active March 1, 2018 21:16
Color wheels using colorspace::hcl by luminance
library(colorspace)
polar2cart <- function(r, theta) # polar to cartesian coords
{
rad <- theta * pi /180
cbind(x = r*cos(rad),
y = r*sin(rad))
}
h <- seq(0, 360, len=150) # hue values
@markheckmann
markheckmann / node_barplot2.R
Last active November 19, 2015 16:34
A tweaked partykit::node_barplot function that adds an additional horizontal line
node_barplot2 <- function(obj,
col = "black",
fill = NULL,
beside = NULL,
ymax = NULL,
ylines = NULL,
widths = 1,
gap = NULL,
reverse = NULL,
id = TRUE,