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 Cylinders option is selected, the first choice in Car option Mazda RX4 also got 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?