Trouble rendering bar graph from ggplot2, using Radio buttons

54 views
Skip to first unread message

Shawn Tang

unread,
Sep 21, 2016, 1:20:45 PM9/21/16
to Shiny - Web Framework for R
Hi all, I've been pounding my head against the the proverbial wall for hours now, and I'm wondering if one of the many geniuses on here could save my forehead from further damage.

Basically, I want to create 3 radio buttons titled 

1: Other
2: LA Orange
3: All

Once each button is clicked, I would like it to generate a ggplot2 bar plot of the relevant data.

1: Other button will use --- OTHERdata
2: LA Orange will use --- LAORANGEdata
3: All will use ---ALLdata

Each of these datasets are defined in the code
The .csv file is also attached to this post


I've tried in my code to just replicate the first graph when the "Other" button is clicked, but to no avail.

The 2 places I probably made an error are:


1: On the server side code, for just the first graph I would like to generate(based on the OTHERdata set), I need a line of code linking the user input to the respective column in the data set.
-------------------------------------------------------------
dat2<-reactive({
      OTHERdata[which(OTHERdata$territory == input$col),]
  })
-------------------------------------------------------------




2: On the server side code, the actual code for generating the graph.

-------------------------------------------------------------
  plot1<- ggplot(dat2(), aes(x=year, y=lossratio,fill=year, label = round(lossratio,2))) +
    geom_bar(stat="identity") +
    xlab("Years") +
    ylab("Loss Ratios") +
    ggtitle("All Other States")+
    geom_label(aes(fill = year),colour = "white", fontface = "bold")
-------------------------------------------------------------

Below is my code in it's entirety, which of course includes the above snippets. 

Could someone tell me what I am doing wrong? As right now when I click the first button for "Other", nothing happens, argh!!!!!!


Thanks much in advance,



--------------------------------------------------------------

df <- read.csv("C:/Users/stang/Desktop/R Directory/Shiny/LR Example.csv", header=TRUE)
territoryfactor<-as.factor(df$Territory)
graphdata<-data.frame(year=c(df$Year),lossratio=c(df$LossRatio), territory=territoryfactor)
graphdata
str(graphdata)

OTHERdata<-subset(graphdata,territory=="Other")
LAORANGEdata<-subset(graphdata, territory=="LA Orange")
ALLdata<-subset(graphdata, territory=="Other" | territory =="LA Orange")

##For the first button, I'm just using OTHERdata
##For the second button, I'm just using LAORAGENdata
##For the third button, I'm just using ALLdata


##UI Code, creating 3 buttons from Radio Button input 
ui<-(fluidPage(
  sidebarLayout(
    sidebarPanel(
 
      radioButtons("col","Switch Plot",
                   choices = c("Other", "LA Orange","All"),
                   selected = "Other")
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.col == 'Other'", plotOutput("hist1")),
      conditionalPanel(
        condition = "input.col == 'LA Orange'", plotOutput("hist2")),
      conditionalPanel(
        condition = "input.col == 'All'", plotOutput("hist3"))
    )
  )
))
  
     
  
  
##Server Code, once each button is clicked, it would produce a bar graph of the applicable subsetted data set above.
##(i.e., graph 1 uses OTHERdata, graph 2 uses LAORANGEdata, graph 3 uses ALLdata)
server<-function(input,output,session){ 
  
  
  output$hist1 <- renderPlot({
  
  
    dat2<-reactive({
      OTHERdata[which(OTHERdata$territory == input$col),]
  })
  
  plot1<- ggplot(dat2(), aes(x=year, y=lossratio,fill=year, label = round(lossratio,2))) +
    geom_bar(stat="identity") +
    xlab("Years") +
    ylab("Loss Ratios") +
    ggtitle("All Other States")+
    geom_label(aes(fill = year),colour = "white", fontface = "bold")
      
   
})
}
  
  
  
shinyApp(ui = ui, server = server)
LR Example.csv

Joe Cheng

unread,
Sep 21, 2016, 5:16:28 PM9/21/16
to Shawn Tang, Shiny - Web Framework for R
1) The definition of dat2 should go outside of the renderPlot--it should be at the same level of indentation as "output$hist1 <- renderPlot({".
2) You're creating the plot1 object, but you're neither returning it nor printing it. Any of these would work:

output$hist1 <- renderPlot({
  ggplot(...) + ...
})

output$hist1 <- renderPlot({
  plot1 <- ggplot(...) + ...
  plot1
})

output$hist1 <- renderPlot({
  plot1 <- ggplot(...) + ...
  print(plot1)
})

But not this:

output$hist1 <- renderPlot({
  plot1 <- ggplot(...) + ...
})



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/ac537616-2082-4cf8-af78-9c833fe11783%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Shawn Tang

unread,
Sep 21, 2016, 5:32:20 PM9/21/16
to Shiny - Web Framework for R, shawn....@gmail.com


On Wednesday, September 21, 2016 at 4:31:06 PM UTC-5, Shawn Tang wrote:
Ah, thanks for the tips Joe, I've reorganized my server side code now to follow your 2 points.

1) I put the dat2<-reactive({... outside of the renderPlot({...
2) I used your 3rd way of printing plot 1

The edited code for the server side is attached below...
But when I run it, it still doesn't print a graph :(

-----------------------------------------------------------------------------
 server<-function(input,output,session){ 
  
  
  dat2<-reactive({
    OTHERdata[which(OTHERdata$territory == input$Other),]
  
  output$hist1 <- renderPlot({
  

  })
  
  plot1<- ggplot(dat2(), aes(x=year, y=lossratio,fill=year, label = round(lossratio,2))) +
    geom_bar(stat="identity") +
    xlab("Years") +
    ylab("Loss Ratios") +
    ggtitle("All Other States")+
    geom_label(aes(fill = year),colour = "white", fontface = "bold")
  print(plot1)
      
   
})
}

----------------------------------------------------------------------------------------------

Joe Cheng

unread,
Sep 21, 2016, 5:38:04 PM9/21/16
to Shawn Tang, Shiny - Web Framework for R
Your braces and parens are all misplaced. Select your whole server function and press Ctrl+i (or Cmd+i if you're on Mac) and you'll see what I mean.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Shawn Tang

unread,
Sep 21, 2016, 5:48:40 PM9/21/16
to Shiny - Web Framework for R, shawn....@gmail.com
Oh that's neat, when I pressed ctrl+i, it certainly indented them better, but still doesn't produce a graph :(

Joe Cheng

unread,
Sep 21, 2016, 6:22:29 PM9/21/16
to Shawn Tang, Shiny - Web Framework for R
server<-function(input,output,session){ 
  dat2<-reactive({
    OTHERdata[which(OTHERdata$territory == input$Other),]
  })
  
  output$hist1 <- renderPlot({
    plot1 <- ggplot(dat2(), aes(x=year, y=lossratio,fill=year, label = round(lossratio,2))) +
      geom_bar(stat="identity") +
      xlab("Years") +
      ylab("Loss Ratios") +
      ggtitle("All Other States")+
      geom_label(aes(fill = year),colour = "white", fontface = "bold")
    print(plot1)
  })
}

Shawn Tang

unread,
Sep 21, 2016, 7:14:13 PM9/21/16
to Shiny - Web Framework for R, shawn....@gmail.com
Sorry Joe, I just realized what you were saying. When I moved the dat2<reactive({....out of the output$hist1<-renderPlot({..., I neglected to correct my parentheses and brackets. Thanks again!
Reply all
Reply to author
Forward
0 new messages