在Shiny中,可以使用updateSelectInput()
函数来更新selectInput
的选项。以下是一个示例代码,演示如何根据条件来更新selectInput
的选项:
library(shiny)
ui <- fluidPage(
selectInput("color", "选择颜色:", choices = c("红色", "蓝色", "绿色")),
actionButton("updateBtn", "更新选项"),
verbatimTextOutput("selectedColor")
)
server <- function(input, output, session) {
output$selectedColor <- renderText({
paste("你选择的颜色是:", input$color)
})
observeEvent(input$updateBtn, {
# 根据条件更新选项
choices <- c("红色", "蓝色")
updateSelectInput(session, "color", choices = choices)
})
}
shinyApp(ui, server)
在上面的示例中,初始的selectInput
包含三个选项:红色、蓝色和绿色。当点击"更新选项"按钮时,observeEvent()
函数会触发,将选项更新为只包含红色和蓝色两个选项。