I would like to create a Shiny app in which the inputs that can be selected depend on the database using the updateSelectInput
function. I want to display the dataframe according different types and to do so, I need to change the value of the multiple
parameter in selectInput
.
To be clearer, this is an example of what I am trying to do:
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 = "Type X",
multiple = FALSE),
selectInput(inputId = "test",
label = "Test",
choices = "",
multiple = TRUE),
circle = TRUE, status = "primary",
icon = icon("gear"),
width = "300px",
tooltip = tooltipOptions(title = "Outils")
),
column(width = 12,
dataTableOutput("data"))
))
)
server <- function(input, output, session) {
observe({
if(input$choice == "Type X"){
updateSelectInput(session,
inputId = "test",
choices = unique(data$foo_name),
selected = NULL)
}
else if(input$choice == "Type Y"){
updateSelectInput(session,
inputId = "test",
choices = unique(data$foo_name),
selected = NULL,
multiple = FALSE)
}
})
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(foo_name %in% input$test)
}
})
}
shinyApp(ui = ui, server = server)
As you can see, when you launch the app and when the type of data is Type X
, everything works fine: the dataframe is reactively displayed according to the inputs. This works because in the selectInput
function, the value of the multiple
parameter is TRUE
.
However, if I want to display data following Type Y
, the app stops working. This is because I set the multiple
parameter as FALSE
.
Apparently, the updateSelectInput
function does not accept that we change the value of the multiple
parameter. Is there a way to bypass it?
As ben pointed out, replacing selectInput
and updateSelectInput
by selectizeInput
and updateSelectizeInput
works. That means that I must remove multiple = FALSE
and add options = list(maxItems = 1)
instead, as explained here .
Here's the fixed code:
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 = "Type X",
multiple = FALSE),
selectizeInput(inputId = "test",
label = "Test",
choices = "",
multiple = TRUE),
circle = TRUE, status = "primary",
icon = icon("gear"),
width = "300px",
tooltip = tooltipOptions(title = "Outils")
),
column(width = 12,
dataTableOutput("data"))
))
)
server <- function(input, output, session) {
observe({
if(input$choice == "Type X"){
updateSelectizeInput(session,
inputId = "test",
choices = unique(data$foo_name),
selected = NULL)
}
else if(input$choice == "Type Y"){
updateSelectizeInput(session,
inputId = "test",
choices = unique(data$foo_name),
selected = NULL,
options = list(maxItems = 1))
}
})
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(foo_name %in% input$test)
}
})
}
shinyApp(ui = ui, server = server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.