Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
@wch
wch / server.r
Created November 6, 2012 06:52
Anorexia experiment data
library(shiny)
# Data preparation ----------------------------------------------------------
library(MASS) # For the anorexia data set
library(reshape2)
# Load the anorexia data set
data(anorexia)
# Give each person a unique ID
@wch
wch / gist:4025734
Created November 6, 2012 16:12
Using plyr with dev_mode

Using the plyr 1.8 Release Candidate in dev_mode()

Using the plyr 1.8 RC with dev_mode() in devtools requires an extra step. This is because devtools depends on stringr, which depends on plyr, and R does not normally let you unload a package that other loaded packages depend on.

If you want to test the new version of plyr in dev mode, first install it with dev_mode():

@wch
wch / server.r
Last active March 18, 2021 02:50
Shiny example with stocks
if (!require(quantmod)) {
stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n")
}
# Download data for a stock if needed, and return the data
require_symbol <- function(symbol, envir = parent.frame()) {
if (is.null(envir[[symbol]])) {
envir[[symbol]] <- getSymbols(symbol, auto.assign = FALSE)
}
@wch
wch / server.r
Created November 6, 2012 20:04
Shiny example with rotating snakes illusion
library(grid)
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
# Image parameters ---------------------------------------------------------
# Number of snakes in x and y directions
nx <- 3
ny <- 3
@wch
wch / heightweight.csv
Created November 7, 2012 20:46
Shiny height-weight example
sex ageYear ageMonth heightIn weightLb
f 11.91667 143 56.3 85
f 12.91667 155 62.3 105
f 12.75 153 63.3 108
f 13.41667 161 59 92
f 15.91667 191 62.5 112.5
f 14.25 171 62.5 112
f 15.41667 185 59 104
f 11.83333 142 56.5 69
f 13.33333 160 62 94.5
@wch
wch / pngtest.r
Created November 8, 2012 02:53
png tests on Linux
# On ubuntu, this is needed to compile Cairo:
# sudo apt-get install libcairo2-dev
# sudo apt-get install libxt-dev
# install.packages('Cairo')
library(ggplot2)
# Regular png
png('test1-png.png')
@wch
wch / gist:4171260
Created November 29, 2012 19:26
Instructions for installing ggplot2 and plyr release candidates

To try out the the release candidate versions of ggplot2 and plyr, run the following code:

install.packages("devtools")
library(devtools)
install_github("plyr", ref = "plyr-1.8-rc")
install_github("gtable", ref = "gtable-0.1.2-rc")
install_github("scales", ref = "scales-0.2.3-rc")
install_github("ggplot2", ref = "ggplot2-0.9.3-rc")
@wch
wch / test.r
Created November 30, 2012 16:23
Code for testing plyr dll loading
# Initial setup ================================
# Start new R session
install.packages('plyr')
library(plyr)
packageVersion('plyr') # Should be 1.7.1
# Should throw error because function isn't exported in 1.7.1
split_indices(c(1L, 1L, 2L), 2L)
@wch
wch / server.r
Last active September 8, 2023 20:25
Shiny example: dynamic input fields
data_sets <- c("mtcars", "morley", "rock")
shinyServer(function(input, output) {
# Drop-down selection box for which data set
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
# Check boxes
@wch
wch / workshop.r
Created December 7, 2012 20:42
R Workshop
Getting started
===============
# Make sure you have ggplot2 and plyr installed
install.packages(c("ggplot2", "plyr"), dep=T)
library(ggplot2)
library(plyr)