Barbara
Barbara

Reputation: 1168

Multiple inputs from checkbox, returning multiple textoutputs

I'm trying to create a shiny app that allows users to select multiple things from a checkbox. Based on the inputs, it should return all the relevant text output fields.

To do this, I'm indexing the checkbox and using multiple criteria in the if statements, but something doesn't work when the input1 is selected: if I select both input2 and input1, then it just shows the result for input2; if I just select input1, then the shiny app crashes.

I tried to add more conditions just to check...but no luck.

Code below:

library(shiny)
library(shinydashboard)


ui <- shinyUI(

  navbarPage("DBC Comparison",
             tabPanel("Stats" ,
                      sidebarLayout(
                        sidebarPanel(

                          checkboxGroupInput("comp_type", "Comparison type", choices = c("input1", "input2", "input3")),

                          actionButton(
                            inputId = "submit_loc",
                            label = "Submit")
                          , width = 3),

                        mainPanel(

                          fluidRow(
                            column(6, textOutput("selected_var1")),
                            #DT::dataTableOutput("table")#, 
                            # div(style = 'overflow-x: scroll', tableOutput('table'))
                            column(6,textOutput("selected_var2"))
                        ), position="left"))
  )
))

##


##
server <- shinyServer(function(input, output) {
  observeEvent(
    eventExpr = input$submit_loc,
    handlerExpr = 
    {
      if(input$comp_type[1] == 'input2' || input$comp_type[2] == 'input2' || (input$comp_type[1] == 'input1' & input$comp_type[2]  == 'input2'))
      {
        output$selected_var2 <- renderText({ 
          "2"
        })}
      else if(input$comp_type[1] == 'input1' ||input$comp_type[2] == 'input1'||input$comp_type[3] == 'input1'|| (input$comp_type[1] == 'input1' & input$comp_type[2]  == 'input2')
                                                                           || (input$comp_type[2] == 'input1' & input$comp_type[1]  == 'input2')
      {
        output$selected_var1 <- renderText({ 
          "1"
        })
        }



    })    




})
##
shinyApp(ui = ui, server = server)

Any ideas?

Upvotes: 1

Views: 1269

Answers (1)

qfazille
qfazille

Reputation: 1671

input$comp_type[2] == 'something' produces NA if you don't have at least 2 items selected. So your if statement return an error.

Also, I try not using render on observe.
I modify your example to use a eventReactive which is easier.

As I didn't get anything about your if conditions, I just wrote some random ones to let you see how I would deal with that.

library(shiny)
library(shinydashboard)


ui <- shinyUI(

  navbarPage("DBC Comparison",
             tabPanel("Stats" ,
                      sidebarLayout(
                        sidebarPanel(

                          checkboxGroupInput("comp_type", "Comparison type", choices = c("input1", "input2", "input3")),

                          actionButton(
                            inputId = "submit_loc",
                            label = "Submit")
                          , width = 3),

                        mainPanel(

                          fluidRow(
                            column(6, textOutput("selected_var"))
                            #DT::dataTableOutput("table")#, 
                            # div(style = 'overflow-x: scroll', tableOutput('table'))
                          ), position="left"))
             )
  ))

##


##
server <- shinyServer(function(input, output) {

  toDisplay <- eventReactive(input$submit_loc, {
    if (all(c("input1", "input2", "input3") %in% input$comp_type)) {
      return("all input selected")
    } else if (all(c("input2", "input3") %in% input$comp_type)) {
      return("input2 and input3 selected")
    } else if ("input1" %in% input$comp_type) {
      return("At least input1 is selected")
    } else {
      return("you are not in a random case I wrote")
    }
  })
  output$selected_var <- renderText({ 
    toDisplay()
  })

})
  ##
  shinyApp(ui = ui, server = server)

Upvotes: 3

Related Questions