Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Forked from wch/server.r
Created October 29, 2012 06:21
Show Gist options
  • Save jcheng5/3971908 to your computer and use it in GitHub Desktop.
Save jcheng5/3971908 to your computer and use it in GitHub Desktop.
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) {
# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
"A title here"
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$tgPlot <- reactivePlot(function() {
# Figure out the aesthetic mappings. color_var requires special handling
# because it can be "none".
if (input$color_var == "") {
aes_mapping <- aes_string(x = input$x_var, y = "len")
} else {
aes_mapping <- aes_string(x = input$x_var, y = "len",
fill = input$color_var)
}
p <- ggplot(tg, mapping = aes_mapping)
if (input$geom_violin) {
p <- p + geom_violin(trim = input$violin_trim,
adjust = input$violin_adjust,
position = position_dodge(input$dodgewidth))
}
if (input$geom_boxplot) {
if (input$boxplot_outliers)
outlier_color <- "black"
else
outlier_color <- NA
p <- p + geom_boxplot(width = input$boxplot_width,
notch = input$boxplot_notch,
outlier.colour = outlier_color,
outlier.size = input$boxplot_outlier_size,
position = position_dodge(input$dodgewidth))
}
if (input$geom_dotplot) {
p <- p + geom_dotplot(binaxis="y", stackdir=input$dotplot_stackdir,
method = input$dotplot_method,
binwidth = input$dotplot_binwidth,
alpha = input$dotplot_alpha,
# Using position=position_dodge() is broken in ggplot2 0.9.2.1.
# So just use "dodge"
position = "dodge")
}
if (input$geom_point) {
p <- p + geom_point(shape = 21, size = input$point_size,
colour = "black",
alpha = input$point_alpha,
position = position_dodge(input$dodgewidth))
}
print(p)
})
})
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Tooth Growth"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
selectInput("x_var", "x variable:",
c("Dose" = "dose",
"Supplement Type" = "supp")),
selectInput("color_var", "fill color variable:",
c("None" = "",
"Dose" = "dose",
"Supplement Type" = "supp"),
selected = "Supplement Type"),
conditionalPanel(
condition = "input.color_var != 'none'",
tags$div(
class = "well",
sliderInput("dodgewidth", "Horizontal dodging width (for different colors):",
min = 0.1, max = 1, value = .9, step = 0.1)
)
),
checkboxInput("geom_point", "Geom: point", FALSE),
conditionalPanel(
condition = "input.geom_point == true",
sliderInput("point_alpha", "Alpha (transparency):",
min = 0.0, max = 1, value = 1, step = 0.1),
sliderInput("point_size", "Size:",
min = 0.5, max = 8, value = 3, step = 0.5)
),
checkboxInput("geom_dotplot", "Geom: dotplot", TRUE),
conditionalPanel(
condition = "input.geom_dotplot == true",
radioButtons("dotplot_method", "Binning method",
c("Dot-density" = "dotdensity",
"Histodot (regular spacing)" = "histodot")),
sliderInput("dotplot_binwidth", "Bin width:",
min = 0.5, max = 3, value = 1, step = 0.25),
sliderInput("dotplot_alpha", "Alpha (transparency):",
min = 0.0, max = 1, value = 1, step = 0.1),
selectInput("dotplot_stackdir", "Stacking direction:",
c("Centered" = "center",
"Centered-aligned" = "centerwhole"))
),
checkboxInput("geom_boxplot", "Geom: boxplot", FALSE),
conditionalPanel(
condition = "input.geom_boxplot == true",
sliderInput("boxplot_width", "Width:",
min = 0.1, max = 1, value = .5, step = 0.1),
checkboxInput("boxplot_notch", "Notched", FALSE),
checkboxInput("boxplot_outliers", "Show outliers", TRUE),
conditionalPanel(
condition = "input.boxplot_outliers == true",
sliderInput("boxplot_outlier_size", "Outlier size:",
min = 1, max = 8, value = 3, step = 1)
)
),
checkboxInput("geom_violin", "Geom: violin", FALSE),
conditionalPanel(
condition = "input.geom_violin == true",
sliderInput("violin_adjust", "Bandwidth adjustment ratio:",
min = 0.25, max = 2, value = 1, step = 0.25),
checkboxInput("violin_trim", "Trim violins to data range", TRUE)
)
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
h3(textOutput("caption")),
plotOutput("tgPlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment