public
Last active

Plotting demo using ggplot2. Check out at http://spark.rstudio.com/dgrapov/1Dplots/.

  • Download Gist
global.R
R
1 2 3 4 5 6 7 8 9 10
#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
}
server.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# 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.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# 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
)
))

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

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

Now it's working. Thanks again.

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.