I am working with a data set with many more rows, here is a snippet of it in the code. I want the app to show the rows correctly, so BMW PETROL 1 will show: fuel=petrol, mpg =30, colour = red and BMW PETROL 2 will show petrol, 40, blue. etc
But when switching between BMW petrol 1 and 2, it does not update correctly. It seems that "Petrol" existing in both rows is causing the problem.
I thought about doing a if/else statement but I think this would become too complicated since I have many rows with multiple petrol/diesel inputs. I also do not want a button that resets the whole thing, I'd like it to change as the user chooses the vehicle.
(I couldn't find an example with the same problem as this but please send me anything that I may have missed!)
Code:
library(shiny)
Data2 <- data.frame(Vehicle= c("BMW PETROL 1", "BMW PETROL 2", "BMW DIESEL" ),
Fuel_type= c("Petrol", "Petrol", "Diesel"),
mpg =c(30, 40, 35),
colour = c("red", "blue", "red"))
ui <- fluidPage(
useShinyjs(),
tags$head(
tags$style(
HTML(" #div_id .selectize-control.single .selectize-input:after{content: none;}"
),
" #container * {display: inline;}"
)
),
sidebarPanel( radioButtons("Radiobutton", "Choose the vehicle", Data2$Vehicle, selected = 1),
tags$div(id= "div_id",
selectizeInput("Select1", "Fuel type", choices= NULL),
selectizeInput("Select2", "WLTP mpg", choices= NULL),
selectizeInput("Select3", "colour", choices= NULL)
)
)
)
server <- function(input, output, session){
observeEvent(input$Radiobutton,{
updateSelectizeInput(session,'Select1',
choices = sort(unique(Data2$Fuel_type[Data2$Vehicle == input$Radiobutton])))
})
observeEvent(input$Select1,{
updateSelectizeInput(session,'Select2',
choices = sort(unique(Data2$mpg[Data2$Vehicle == input$Radiobutton &
Data2$Fuel_type == input$Select1 ])))
})
observeEvent(input$Select2,{
updateSelectizeInput(session,'Select3',
choices= sort(unique(Data2$colour[Data2$Vehicle == input$Radiobutton &
Data2$Fuel_type == input$Select1 &
Data2$mpg == input$Select2])))
})
}
shinyApp(ui = ui, server = server)