I'm trying to display some UI under conditions. I would like the way to choose input to vary according to the type of data: when the type selected is Type X
, I want to display a selectizeInput
and when Type Y
is selected, I want to display a sliderInput
. To do so, I use uiOutput
in conditionalPanel
.
Here's what I've done so far, following this question:
library(shiny)
library(shinyWidgets)
library(WDI)
library(DT)
library(dplyr)
foo <- data.frame(foo_name = c("A", "A", "B", "B", "C", "C"))
data <- cbind(head(mtcars), foo)
ui <- navbarPage(position = "static-top",
tabPanel(title = "Base 1",
fluidRow(
dropdownButton(
selectInput(inputId = "choice",
label = "Type of data",
choices = c("Type X",
"Type Y"),
selected = NULL,
multiple = FALSE),
conditionalPanel(
condition = "input$choice == 'Type X'",
uiOutput("machin")
),
conditionalPanel(
condition = "input$choice == 'Type Y'",
uiOutput("machin2")
),
circle = TRUE, status = "primary",
icon = icon("gear"),
width = "300px",
tooltip = tooltipOptions(title = "Outils")
),
column(width = 12,
dataTableOutput("data"))
))
)
server <- function(input, output) {
output$machin <- renderUI({
selectizeInput(inputId = "test",
label = "test",
choices = unique(data$foo_name),
selected = NULL,
multiple = T)
})
output$machin2 <- renderUI({
sliderInput("test2",
"Test2",
min = min(data$hp),
max = max(data$hp),
value = c(min(data$hp),
max(data$hp))
)
})
output$data <- renderDataTable({
if(input$choice == "Type X"){
data2 <- data %>%
filter(foo_name %in% input$test)
}
else if(input$choice == "Type Y"){
data3 <- data %>%
filter(hp %in% input$test2)
}
})
}
shinyApp(ui = ui, server = server)
Several problems here:
- Both types of inputs are displayed whatever the type of data selected
- when Type X is selected, the dataframe is empty (since the
selectizeInput
is NULL) but when Type Y is selected, some values of the dataframe are displayed
How can I fix this?