Skip to content

Instantly share code, notes, and snippets.

@ptoche
Last active January 2, 2016 13:59
Show Gist options
  • Save ptoche/8313943 to your computer and use it in GitHub Desktop.
Save ptoche/8313943 to your computer and use it in GitHub Desktop.
minor edits to the very awesome app seen here: https://gist.github.com/jcheng5/3971908
# server.R
library("shiny")
library("datasets")
library("ggplot2")
tg <- ToothGrowth
tg$dose <- factor(tg$dose)
shinyServer(function(input, output) {
# Return the formula text for printing as a caption
output$Caption <- renderText({
paste(input$title)
})
# Dynamic plot of selected variable against mpg
output$Plot <- renderPlot({
# 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)
}
# Make basic ggplot
p <- ggplot(tg, mapping = aes_mapping)
# Add violin plot layer
if (input$geom_violin) {
p <- p + geom_violin(
trim = input$violin_trim
, adjust = input$violin_adjust
, position = position_dodge(input$dodgewidth)
)
}
# Add box plot layer
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)
)
}
# Add dot plot layer
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
, position = "dodge"
)
}
# Add geom point layer
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)
)
}
# return the plot with selected layers
print(p)
}) # close output$outputPlot
}) # close shinyServer(function{
# ui.R
library("shiny")
shinyUI(
pageWithSidebar(
headerPanel("Tooth Growth")
,
sidebarPanel(
textInput(
inputId = "title"
, label = "Enter a title here:"
, value = "plot"
)
,
selectInput(
inputId = "x_var"
, label = "x variable:"
, choices = c("Dose" = "dose", "Supplement Type" = "supp")
)
,
selectInput(
inputId = "color_var"
, label = "fill color variable:"
, choices = c("None" = "", "Dose" = "dose", "Supplement Type" = "supp")
, selected = "Supplement Type"
)
,
conditionalPanel(
condition = "input.color_var != 'none'"
, tags$div( class = "well"
, sliderInput(
inputId = "dodgewidth"
, label = "Horizontal dodging width (for different colors):"
, min = 0.1
, max = 1
, value = .9
, step = 0.1
)
)
)
,
checkboxInput(inputId = "geom_point", label = "Geom: point", value = FALSE)
,
conditionalPanel(
condition = "input.geom_point == true"
, sliderInput(
inputId = "point_alpha"
, label = "Alpha (transparency):"
, min = 0.0
, max = 1
, value = 1
, step = 0.1
)
, sliderInput(
inputId = "point_size"
, label = "Size:"
, min = 0.5
, max = 8
, value = 3
, step = 0.5
)
)
,
checkboxInput(inputId = "geom_dotplot", label = "Geom: dotplot", value = TRUE)
,
conditionalPanel(
condition = "input.geom_dotplot == true"
, radioButtons(
inputId = "dotplot_method"
, label = "Binning method"
, choices = c("Dot-density" = "dotdensity", "Histodot (regular spacing)" = "histodot")
)
,
sliderInput(
inputId = "dotplot_binwidth"
, label = "Bin width:"
, min = 0.5
, max = 3
, value = 1
, step = 0.25
)
,
sliderInput(
inputId = "dotplot_alpha"
, label = "Alpha (transparency):"
, min = 0.0
, max = 1
, value = 1
, step = 0.1
)
,
selectInput(
inputId = "dotplot_stackdir"
, label = "Stacking direction:"
, choices = c("Centered" = "center", "Centered-aligned" = "centerwhole")
)
)
,
checkboxInput(inputId = "geom_boxplot", label = "Geom: boxplot", value = FALSE)
,
conditionalPanel(
condition = "input.geom_boxplot == true"
, sliderInput(
inputId = "boxplot_width"
, label = "Width:"
, min = 0.1
, max = 1
, value = .5
, step = 0.1
)
,
checkboxInput(inputId = "boxplot_notch", label = "Notched", value = FALSE)
,
checkboxInput(inputId = "boxplot_outliers", label = "Show outliers", value = TRUE)
,
conditionalPanel(
condition = "input.boxplot_outliers == true"
, sliderInput(
inputId = "boxplot_outlier_size"
, label = "Outlier size:"
, min = 1
, max = 8
, value = 3
, step = 1
)
)
)
,
checkboxInput(inputId = "geom_violin", label = "Geom: violin", value = FALSE)
,
conditionalPanel(
condition = "input.geom_violin == true"
, sliderInput(
inputId = "violin_adjust"
, label = "Bandwidth adjustment ratio:"
, min = 0.25
, max = 2
, value = 1
, step = 0.25
)
, checkboxInput(inputId = "violin_trim", label = "Trim violins to data range", value = TRUE)
)
) # close sidebarPanel(
,
mainPanel(
h3(textOutput("Caption"))
,
plotOutput("Plot")
) # close mainPanel(
) # close pageWithSidebar(
) # close shinyUI(
@ptoche
Copy link
Author

ptoche commented Jan 8, 2014

Learning stuff from an app by Winston Chang & Joe Cheng,

https://gist.github.com/wch/3969102

https://gist.github.com/jcheng5/3971908

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment