r - R Shiny:使用 updateSelectInput 更改多个参数的值?
问题描述
我想创建一个闪亮的应用程序,其中可以选择的输入取决于使用该updateSelectInput
函数的数据库。multiple
我想根据不同的类型显示数据框,为此,我需要更改selectInput
.
为了更清楚,这是我正在尝试做的一个例子:
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)
如您所见,当您启动应用程序并且数据类型为 时Type X
,一切正常:数据框根据输入进行响应式显示。这是有效的,因为在selectInput
函数中,multiple
参数的值为TRUE
。
但是,如果我想显示以下数据Type Y
,则应用程序将停止工作。这是因为我将multiple
参数设置为FALSE
.
显然,该updateSelectInput
函数不接受我们更改multiple
参数的值。有没有办法绕过它?
解决方案
正如本指出的那样,用selectInput
andupdateSelectInput
替换selectizeInput
andupdateSelectizeInput
可以工作。这意味着我必须删除multiple = FALSE
并options = list(maxItems = 1)
改为添加,如此处所述。
这是固定代码:
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)
推荐阅读
- volttron - get_multiple_points volttron RPC 调用
- c++ - 为什么 main() 函数总是 int 而不是 long?
- android - 如何在 Android Studio 中做浮动提示/文本?
- c++ - 已解决:如何避免在 C++ 中创建临时向量?
- open-policy-agent - 为什么 OPA/Rego 中的“违反”和“拒绝”之间有确切的区别?
- python - 每次单击链接时,如何将数据库行增加 1?
- sql - 如何在 BigQuery 中使用空间连接进行 OUTER 连接,以便能够在没有交集的情况下获得 0 的计数值?
- vue.js - 如何测试在 Vue 组件挂载函数中调用 Vuex 模块动作?
- css - 使用 css 如何与不同屏幕尺寸的列表保持一致的距离?
- sql-server - MS Access 子表单组合框在链接到 SQL Server 表时消失