0
votes

I would like to select an option which has an effect on the output. Here there are two options: Car, Cylinders to choose. Based on the selected option, they have different choices to select.

library(shiny)

mtcars$cars <- rownames(mtcars)

ui <- fluidPage(
  title = 'Select experiment',
  sidebarLayout(
    sidebarPanel(
      radioButtons("selectOpt", "Want to select:",
                   list("Car"='car', "Cylinders"='cyl'),
                   selected = 'car'),

      conditionalPanel(
        condition = "input.selectOpt=='car'",
        selectizeInput(
          inputId = "sel_car", 
          label = "Select a Car",
          choices = mtcars$cars
        )),
      conditionalPanel(
        condition = "input.selectOpt=='cyl'",
        selectizeInput(
          inputId = "sel_cyl", 
          label = "Select Cylinders",
          choices = unique(mtcars$cyl)
        )
        )
    ),
    mainPanel(
      helpText('Output of the examples in the left:'),
      verbatimTextOutput('ex_out')
    )
  )
)

server <- function(input, output) {

    output$ex_out <- renderPrint({
      if(!is.null(input$sel_car)){
        cat(input$sel_car,input$sel_cyl,'\n')
        mtcars[mtcars$cars == input$sel_car,]
      }else if(!is.null(input$sel_cyl)){
        mtcars[mtcars$cyl == input$sel_cyl,]
      }
    })
}

shinyApp(ui, server)

However, I observed that always these two options have an option selected. This can be observed in the first line of the output(source cat(input$sel_car,input$sel_cyl,'\n')).

When Car is selected, the first choice in Cylinders option 6 also got selected. When **Car** is selected

When Cylinders option is selected, the first choice in Car option Mazda RX4 also got selected. When **Cylinders** option is selected

How to avoid the default selection fo the first choice, such that I would be able to evaluate both if and else if sections?

1

1 Answers

1
votes

Single choice select(ize)Input() always have a selected option. From ?selectInput:

selected
The initially selected value (or multiple values if multiple = TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.

To obtain the functionality you're after we can insert a sort of 'invalid' choice:

selectizeInput(
  inputId = "sel_car", 
  label = "Select a Car",
  choices = c('No car' = 0, mtcars$cars)
)

And check for that in the if.. else:

if(input$sel_car != 0){
  cat(input$sel_car,input$sel_cyl,'\n')
  mtcars[mtcars$cars == input$sel_car,]
}

Complete code:

library(shiny)

mtcars$cars <- rownames(mtcars)

ui <- fluidPage(
  title = 'Select experiment',
  sidebarLayout(
    sidebarPanel(
      radioButtons("selectOpt", "Want to select:",
                   list("Car"='car', "Cylinders"='cyl'),
                   selected = 'car'),

      conditionalPanel(
        condition = "input.selectOpt=='car'",
        selectizeInput(
          inputId = "sel_car", 
          label = "Select a Car",
          choices = c('No car' = 0, mtcars$cars)
        )),
      conditionalPanel(
        condition = "input.selectOpt=='cyl'",
        selectizeInput(
          inputId = "sel_cyl", 
          label = "Select Cylinders",
          choices = c('No cylinder selected' = 0, unique(mtcars$cyl)),
          selected = NA
        )
      )
    ),
    mainPanel(
      helpText('Output of the examples in the left:'),
      verbatimTextOutput('ex_out')
    )
  )
)

server <- function(input, output) {

  output$ex_out <- renderPrint({
    cat(input$sel_car)
    if(input$sel_car != 0){
      cat(input$sel_car,input$sel_cyl,'\n')
      mtcars[mtcars$cars == input$sel_car,]
    }else if(input$sel_cyl != 0){
      mtcars[mtcars$cyl == input$sel_cyl,]
    }
  })
}

shinyApp(ui, server)