Multiple "checkboxGroupInput" not working correctly.

688 views
Skip to first unread message

Shawn Tang

unread,
Sep 27, 2016, 10:22:32 AM9/27/16
to Shiny - Web Framework for R
Good morning, I'm trying to create a series of graphs(via ggplot) that are reactive based on user selections from the two "checkboxGroupInput" inputs.

One of the checkboxGroupInput is for the Territory, and the other is to choose the NOB(group1 or group2).

So for example, if the user selects a Territory of "Other", and a NOB of "group1", I would expect the graph to display only those observations that fulfill both conditions, being in "Other" AND "group1". This is working.

What is not working is the following example:
The user selects a Territory of "Other", and a NOB of "group1", and NOB of "group2". 
I would expect the graph to display only those observations in "Other" AND in ("group1" OR "group2").
This is the part that is not working.
Right now if I select those 3 choices, it gives me half of the observations that I want to be taken into account.

The CSV I'm using is attached. Could someone with more experience than me(probably most of you), shed some light on why my code doesn't produce the desired outcome?

My code is attached below.

A thousand thanks!

--------------------------------------------------
library("shiny")
library("ggplot2")



##Read in the file from local directory.
data<-read.csv("C:/Users/stang/Desktop/R Directory/Shiny/LR Example.csv", header=TRUE)


##UI side logic
ui<-fluidPage(
  
  headerPanel("Selections:"),
  
  ##Separates 
  sidebarPanel(
  
    checkboxGroupInput("Territory", 
                           
                           label = "Select a Geography",
                           choices = c("Select All","ALL","LA Orange","Other")),
  
    checkboxGroupInput("Nature",
                          
                          label = "Select a Nature of Business",
                          choices = c("group1","group2"))),
  
  
  
  
  mainPanel(
              plotOutput("LR_Plot"),
              verbatimTextOutput("stats")))
               



#Server side logic
server<-function(input,output) {
            
          data.new<-reactive({subset(data,data$Territory==input$Territory & data$Nob==input$Nature)})
          
          
 
          
          output$LR_Plot<-renderPlot({
              
              plot1<-ggplot(data.new(), aes(x=Year, y=LossRatio,fill=Year, label = round(LossRatio,2))) +
                geom_bar(stat="identity") +
                xlab("Years") +
                ylab("Loss Ratios") +
                ggtitle("LA and Orange County")+
                geom_label(aes(fill = Year),colour = "white", fontface = "bold") 
              
              print(plot1)
              })
          
   
          
          
          output$stats<-renderPrint({summary(data.new())})
}



#Connects the UI to Server
shinyApp(ui=ui,server=server)

-----------------------------------------------
LR Example.csv

David Ruvolo

unread,
Sep 28, 2016, 11:34:47 AM9/28/16
to Shiny - Web Framework for R
Hi - 

You will need to add additional steps to data.new in order to match multiple criteria (e.g, if "ALL" and "Other" + "Group1" and "Group2" are selected) across multiple columns (Territory and Nob).

One way to do this is to use the %in%  for subsetting data. In the data.new section, try adding ...

data.new<-reactive({
       
       
#' set a default
       
if(is.null(input$Territory) & is.null(input$Nature)){

           
return(data) ## simply return the data

       
} else {

            data
[data$Territory %in% input$Territory & data$Nob %in% input$Nature,]

       
}

   
})


A quick note:
  • I added a default action for when no selection is made (i.e., first load)
Hope that helps!

- DCR
Reply all
Reply to author
Forward
0 new messages