Skip to content

Instantly share code, notes, and snippets.

@dgrapov
Last active September 30, 2022 01:58
Show Gist options
  • Save dgrapov/5792778 to your computer and use it in GitHub Desktop.
Save dgrapov/5792778 to your computer and use it in GitHub Desktop.
Plotting demo using ggplot2. Check out at http://spark.rstudio.com/dgrapov/1Dplots/.
#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
output$variable <- renderUI({
obj<-switch(input$dataset,
"iris" = iris,
"mtcars" = mtcars)
var.opts<-namel(colnames(obj))
selectInput("variable","Variable:", var.opts) # uddate UI
})
output$group <- renderUI({
obj<-switch(input$dataset,
"iris" = iris,
"mtcars" = mtcars)
var.opts<-namel(colnames(obj))
selectInput("group","Groups:", var.opts) # uddate UI
})
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({
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")
),
uiOutput("variable"), # depends on dataset ( set by output$variable in server.R)
uiOutput("group"), # depends on dataset ( set by output$group in server.R)
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
)
))
@dgrapov
Copy link
Author

dgrapov commented Jun 16, 2013

Test of Shiny interface for dynamic UI and plotting using ggplot2.

@subasish
Copy link

Thanks for the code.

From local drive, somehow the code's not working. Showing the following message:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

@subasish
Copy link

Now it's working. Thanks again.

@EconometricsBySimulation

I don't know if it matters but it seems to be working for me. Thanks for the code!

@dotRData
Copy link

is it possible to update output$group dependent on output$variable ?

@waltermblair
Copy link

This is beautiful, thank you!

@Dev1412
Copy link

Dev1412 commented Dec 8, 2016

This is great and very neat. Thank you!!
Do you have a similar example of a line chart with multiple y axis to share, where the input variables are taken from the user, data is reformatted and then line chart created?

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