簡體   English   中英

基於單選按鈕的 selectizeInput 的閃亮更新選擇

[英]Shiny update choices of selectizeInput based on radio buttons

我試圖根據用戶是單擊“通用名稱”還是“科學名稱”按鈕來更新selectizeInput()的選項。 默認值為“通用名稱”。

我從這個答案中知道conditionalPanel() ,但我的選擇將鏈接到輸出圖,所以我需要它們是反應式的。 因此,在單擊“科學名稱”時,我希望清除當前的選擇,然后只有新的選擇 (names_vector2) 可供選擇。 同樣,如果用戶然后單擊回到“通用名稱”,我希望清除當前選擇,並且只有來自 names_vector1 的選擇可供選擇。

希望這是有道理的!

library(shiny)
library(shinyWidgets)

names_vector1 = paste0("common", 1:10)
names_vector2 = paste0("scientific", 1:10)

ui = fluidPage(
  fluidRow(
    selectizeInput(
      inputId = "species_selector",
      label = "Choose a species:",
      selected = "common1",
      choices = c("Choose" = "", names_vector1),
      options = list(
        maxOptions = 5,
        maxItems = 4
      )
    ),
    awesomeRadio(
      inputId = "species_selector_type",
      label = NULL,
      choices = c("Common name","Scientific name"),
      selected = "Common name",
      inline = TRUE
    )
  )
)

server = server = server = function(input, output, session){
 
  # I want to change the selectizeInput choices as the user clicks the buttons:
  # "Common name" and "Scientific name"
  observeEvent(input$species_selector_type {
    
    if (input$species_selector_type == "Scientific name")
    updateSelectizeInput(
      session,
      inputId = "species_selection",
      choices = c("Choose" = "", names_vectors),
    )
  })
  # The desired result is to:
  # 1. Clear the current selectiveInput selected names each time a new button is clicked
  # 2. Update the choices so that:
        # Common name = names_vector1
        # Scientific name = names_vector2
}

shinyApp(ui, server)

你快到了 - 添加了一個 else if 語句:

library(shiny)
library(shinyWidgets)

names_vector1 = paste0("common", 1:10)
names_vector2 = paste0("scientific", 1:10)

ui = fluidPage(fluidRow(
  selectizeInput(
    inputId = "species_selector",
    label = "Choose a species:",
    selected = "common1",
    choices = c("Choose" = "", names_vector1),
    options = list(maxOptions = 5,
                   maxItems = 4)
  ),
  awesomeRadio(
    inputId = "species_selector_type",
    label = NULL,
    choices = c("Common name", "Scientific name"),
    selected = "Common name",
    inline = TRUE
  )
))

server = server = server = function(input, output, session) {
  observeEvent(input$species_selector_type, {
    if (input$species_selector_type == "Common name") {
      updateSelectizeInput(session,
                           inputId = "species_selector",
                           choices = c("Choose" = "", names_vector1))
    } else if (input$species_selector_type == "Scientific name") {
      updateSelectizeInput(session,
                           inputId = "species_selector",
                           choices = c("Choose" = "", names_vector2))
    }
  })
}

shinyApp(ui, server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM