【发布时间】:2021-03-23 05:45:51
【问题描述】:
我正在尝试简化 Shiny 应用中的搜索功能,并注意到响应式功能不断重复并覆盖我想要显示的内容。
我想让用户搜索两个变量:1) 县和 2) 邮政编码。我希望根据选中的单选按钮(县或邮政编码)更改搜索框上方的标签,理想情况下减少可以选择的可用选项列表.
因此,如果您选择邮政编码,您会看到一个邮政编码列表,如果您选择县,您只会看到县列表。从下面的代码可以看出,它在输出方面有些功能,但动态标签和动态选择列表并不总是有效。
library(shiny)
library(dplyr)
library(tidyverse)
library(tibble)
library(DT)
county_list = c('Kings','Rockland','Orange','New York','Richmond','Kings','Rockland','Orange','New York','Richmond')
zip_list = c('11230','10901','12550','10023','10044','11230','10901','12550','10023','10044')
store_name = c('Store 1','Store 2', 'Store 3','Store 4', 'Store 5','Store 1','Store 7', 'Store 8','Store 9', 'Store 10')
db = tibble(Store = store_name ,County = county_list, ZipCode = zip_list)
ui <- fluidPage(
titlePanel("Title"),
tabsetPanel(
tabPanel('Data',
radioButtons("county_or_zip_select","Choose:",choices=c("County","ZipCode")),
selectInput("search4_all", "search_box:", selectize = FALSE,choices=unique(c(county_list,zip_list))),
DT::DTOutput('data')
)
))
server <- function(input, output, session) {
getData <- reactive({
if(input$county_or_zip_select=='County'){
df<-db[grep(input$search4_all, db$County,ignore.case = T),]
updateSelectInput(session,"search4_all",label='Select County:')
}else{
df<-db[grep(input$search4_all, db$ZipCode, ignore.case = T),]}
updateSelectInput(session,"search4_all",label='Select Zip Code:')
df
})
output$data <- DT::renderDT(getData(),options = list(lengthMenu = c(5, 10), pageLength = 5)) # Refers to DT::DTOutput('data') in main panel section
}
shinyApp(ui = ui, server = server)
注意:前几天我发布了一个类似的question,我已经删除(或尝试删除),但我没有合并updatSelectInput,更重要的是,我没有提供@987654322 @正如所指出的。对此感到抱歉。
【问题讨论】:
标签: r shiny shiny-reactivity