Create a gist now

Instantly share code, notes, and snippets.

@dgrapov /global.R
Last active Dec 8, 2016

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@dgrapov

dgrapov Jun 16, 2013

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

Owner

dgrapov commented Jun 16, 2013

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

@subasish

This comment has been minimized.

Show comment Hide comment
@subasish

subasish Jun 18, 2013

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=)?

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

This comment has been minimized.

Show comment Hide comment
@subasish

subasish Jun 18, 2013

Now it's working. Thanks again.

Now it's working. Thanks again.

@EconometricsBySimulation

This comment has been minimized.

Show comment Hide comment
@EconometricsBySimulation

EconometricsBySimulation Jun 20, 2013

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

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

@ranandiitk

This comment has been minimized.

Show comment Hide comment
@ranandiitk

ranandiitk Feb 10, 2016

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

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

@waltermblair

This comment has been minimized.

Show comment Hide comment
@waltermblair

waltermblair Dec 6, 2016

This is beautiful, thank you!

This is beautiful, thank you!

@Dev1412

This comment has been minimized.

Show comment Hide comment
@Dev1412

Dev1412 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?

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