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 December 17, 2012 18:35
Shiny example: checkboxInputGroup doesn't update when number selected goes to zero
shinyServer(
function(input, output) {
output$result <- reactivePrint( function() {
length(input$vars)
})
}
)
@wch
wch / .gitignore
Created December 20, 2012 22:20
ggplot2 evaluation in Shiny
# History files
.Rhistory
# Example code in package build process
*-Ex.R
shinyServer(function(input, output) {
output$main_plot <- reactivePlot(function() {
plot(1:10, 1:10)
})
})
@wch
wch / server.r
Last active December 13, 2015 19:38
Shiny demo of actionButton
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# Take a dependency on input$goButton
input$goButton
# Use isolate() to avoid dependency on input$obs
dist <- isolate(rnorm(input$obs))
hist(dist)
})
})
@wch
wch / shinyserialize.r
Last active December 14, 2015 00:59
Code for serializing Shiny applications, so they can be easy distributed and run with copy-and-paste
# The inverse of shinyDeserialize(): this function will read files from a Shiny
# application and convert them to quoted expressions and strings, and place
# those within a call to shinyRun().
# This function returns the seralized app as a string, and also writes it to
# outfile. By default, outfile is stdout (the console). You can set it to
# a filename, or NULL for no output.
shinySerialize <- function(appdir = NULL, outfile = stdout()) {
if (is.null(appdir))
stop("Need a path")
@wch
wch / gist:5006372
Last active December 14, 2015 01:28
Message to shiny-discuss about shinySerialize

Hi everyone -

This mailing list is great for communicating ideas and problems, but when it comes to sharing code examples, it can be a bit tedious for app authors to cut and paste their files into an email message, and it can be even more tedious for readers to re-create the files from the email message.

I've written a set of functions that makes this easier:

  • shinySerialize() turns your Shiny app into text that can be cut-and-pasted into an email message.
  • shinyDeserialize() does the reverse: it turns that text into a set of files, in an application directory (and subdirectories, if needed).
  • shinyRun() is a wrapper function that first calls shinyDeserialize() to write out the app, then calls runApp() to run it.

These functions aren't part of the Shiny package; for now they're just things I've been playing with. They're defined here:

@wch
wch / examples.r
Created March 6, 2013 20:31
Examples for O'Reilly Webinar on Data Visualization with R and ggplot2
Getting started
===============
# Make sure you have ggplot2 installed
install.packages("ggplot2")
library(ggplot2)
Basic examples
==============
@wch
wch / server.r
Last active December 14, 2015 16:49
Shiny clientData test app
shinyServer(function(input, output, clientData) {
output$image <- renderImage({
width <- clientData$output_plot_width
height <- clientData$output_plot_height
# A temp file to save the output. This file will be removed later by renderImage
outfile <- tempfile(fileext='.png')
png(outfile, width=width, height=height)
@wch
wch / circles.r
Created March 8, 2013 21:36
Tests for PNG and PDF output in R
library(grid)
makecircles <- function(gp) {
grid.rect(gp=gpar(fill="grey60"))
n <- 40
grid.circle(x=seq(0.1, 0.9, length=n),
y=0.5 + 0.4*sin(seq(0,2*pi, length=n)),
r=abs(0.1*cos(seq(0,2*pi, length=n))),
gp=gp)
}
@wch
wch / server.r
Created March 13, 2013 16:24
clientData example for Shiny
shinyServer(function(input, output, clientData) {
output$image <- renderImage({
width <- clientData$output_plot_width
height <- clientData$output_plot_height
# A temp file to save the output. This file will be removed later by renderImage
outfile <- tempfile(fileext='.png')
png(outfile, width=width, height=height)