Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
@wch
wch / gist:3250485
Created August 3, 2012 19:06
Creating a new scale for ggplot2
# 1. functions for yearmon transformer ================
# Convert from numeric to yearmon
to_yearmon <- function(x) as.yearmon(x)
# Inverse: convert from yearmon to numeric
from_yearmon <- function(x) {
if (!inherits(x, "yearmon")) {
stop("Invalid input: date_yearmon works with objects of class yearmon only",
call. = FALSE)
@wch
wch / unlockEnvironment.r
Created August 7, 2012 01:17
Sample code for unlocking environments in R
library(inline)
inc <- '
/* This is taken from envir.c in the R 2.15.1 source
https://github.com/SurajGupta/r-source/blob/master/src/main/envir.c
*/
#define FRAME_LOCK_MASK (1<<14)
#define FRAME_IS_LOCKED(e) (ENVFLAGS(e) & FRAME_LOCK_MASK)
#define UNLOCK_FRAME(e) SET_ENVFLAGS(e, ENVFLAGS(e) & (~ FRAME_LOCK_MASK))
'
@wch
wch / controlexamples.Rmd
Created September 24, 2012 19:35
Examples for controls
Examples for controls
========================================================
```{r tidy=FALSE, fig.width=4, fig.height=3 }
library(ggplot2)
# Base plot
p <- ggplot(ToothGrowth, aes(x=factor(dose), y=len, fill=supp)) +
scale_fill_manual(values=c("#E69F00", "#377EB8"))
@wch
wch / keytest.applescript
Created September 26, 2012 17:00
Keyboard input speed test for RStudio
tell application "RStudio"
activate
tell application "System Events"
tell application process "RStudio"
keystroke "t0 <- Sys.time()"
key code 36 using command down
key code 36
keystroke "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf"
key code 36
keystroke "t1 <- Sys.time(); t1-t0"
@wch
wch / maptest.r
Created September 26, 2012 21:58
Map alignment test
library(gcookbook)
library(ggplot2)
# Take a slice where z is equal to the minimum value of z
islice <- subset(isabel, z == min(z))
# Just the hurricane data
ggplot(islice, aes(x=x, y=y, fill=speed)) + geom_raster(alpha=.5) +
scale_fill_continuous(na.value=NA)
@wch
wch / server.r
Created October 28, 2012 16:38
Experiments with shiny and ggplot2
library(shiny)
library(datasets)
library(ggplot2)
tg <- ToothGrowth
tg$dose <- factor(tg$dose)
# Define server logic
shinyServer(function(input, output) {
@wch
wch / cartogram-labels.csv
Created October 30, 2012 21:14
Election data example
State Abbrv Longitude Latitude
california CA 3 12
washington WA 3 18.5
oregon OR 4 17
nevada NV 5 15.5
idaho ID 7 16.5
utah UT 7 14.5
arizona AZ 8 9
montana MT 9 17.5
wyoming WY 9 15.5
@wch
wch / server.r
Created November 6, 2012 02:25
Basic histogram example
library(shiny)
shinyServer(function(input, output) {
output$main_plot <- reactivePlot(function() {
hist(faithful$waiting,
breaks = as.numeric(input$n_breaks),
xlab = "Time to next eruption (minutes)",
main = "Waiting times for Old Faithful geyser")
@wch
wch / server.r
Created November 6, 2012 03:00
Second histogram example, with density estimate
shinyServer(function(input, output) {
output$main_plot <- renderPlot(width = 400, height = 300, {
hist(faithful$eruptions,
probability = TRUE,
breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)",
main = "Geyser eruption duration")
@wch
wch / server.r
Created November 6, 2012 06:51
Scatter plot with model fit lines
library(shiny)
shinyServer(function(input, output) {
output$main_plot <- reactivePlot(function() {
plot(x = faithful$eruptions, y = faithful$waiting,
xlab = "Eruption duration (minutes)",
ylab = "Waiting to next eruption (minutes)",
main = "Eruptions of Old Faithful geyser")