【发布时间】:2021-12-13 13:42:44
【问题描述】:
我正在尝试根据用户是单击“通用名称”还是“学名”按钮来更新 selectizeInput() 中的选项。默认为“通用名”。
我从this answer 知道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)
【问题讨论】:
标签: r shiny reactive observers selectinput