Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Forked from dgrapov/global.R
Last active November 30, 2022 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcheng5/5929787 to your computer and use it in GitHub Desktop.
Save jcheng5/5929787 to your computer and use it in GitHub Desktop.
#initialize
library(datasets)
library(ggplot2)
#helper function (convert vector to named list)
namel<-function (vec){
tmp<-as.list(vec)
names(tmp)<-as.character(unlist(vec))
tmp
}
# shiny server side code for each call
shinyServer(function(input, output, session){
#update variable and group based on dataset
observe({
if (is.null(input$dataset))
return()
obj<-switch(input$dataset,
"iris" = iris,
"mtcars" = mtcars)
var.opts<-namel(colnames(obj))
updateSelectInput(session, "variable", choices = var.opts)
updateSelectInput(session, "group", choices = var.opts)
})
output$caption<-renderText({
switch(input$plot.type,
"boxplot" = "Boxplot",
"histogram" = "Histogram",
"density" = "Density plot",
"bar" = "Bar graph")
})
output$plot <- renderUI({
plotOutput("p")
})
#plotting function using ggplot2
output$p <- renderPlot({
variable <- get(input$dataset)[[input$variable]]
group <- get(input$dataset)[[input$group]]
if (is.null(variable) || is.null(group))
return(NULL)
plot.obj<<-list() # not sure why input$X can not be used directly?
plot.obj$data<<-get(input$dataset)
plot.obj$variable<<-with(plot.obj$data,get(input$variable))
plot.obj$group<<-with(plot.obj$data,get(input$group))
#dynamic plotting options
plot.type<-switch(input$plot.type,
"boxplot" = geom_boxplot(),
"histogram" = geom_histogram(alpha=0.5,position="identity"),
"density" = geom_density(alpha=.75),
"bar" = geom_bar(position="dodge")
)
require(ggplot2)
#plotting theme
.theme<- theme(
axis.line = element_line(colour = 'gray', size = .75),
panel.background = element_blank(),
plot.background = element_blank()
)
if(input$plot.type=="boxplot") { #control for 1D or 2D graphs
p<-ggplot(plot.obj$data,
aes(
x = plot.obj$group,
y = plot.obj$variable,
fill = as.factor(plot.obj$group)
)
) + plot.type
if(input$show.points==TRUE)
{
p<-p+ geom_point(color='black',alpha=0.5, position = 'jitter')
}
} else {
p<-ggplot(plot.obj$data,
aes(
x = plot.obj$variable,
fill = as.factor(plot.obj$group),
group = as.factor(plot.obj$group),
#color = as.factor(plot.obj$group)
)
) + plot.type
}
p<-p+labs(
fill = input$group,
x = "",
y = input$variable
) +
.theme
print(p)
})
})
# UI for app
shinyUI(pageWithSidebar(
# title
headerPanel("Select Options"),
#input
sidebarPanel
(
selectInput("dataset","Data:",
list(iris = "iris", mtcars = "mtcars")
),
selectInput("variable","Variable:", "Loading..."),
selectInput("group","Group:", "Loading..."),
selectInput("plot.type","Plot Type:",
list(boxplot = "boxplot", histogram = "histogram", density = "density", bar = "bar")
),
checkboxInput("show.points", "show points", TRUE)
),
# output
mainPanel(
h3(textOutput("caption")),
#h3(htmlOutput("caption")),
uiOutput("plot") # depends on input
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment