【问题标题】:How to stop reactive functions from repeating in Shiny?如何阻止反应函数在 Shiny 中重复?
【发布时间】: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


    【解决方案1】:

    您可以创建两个单独的selectInput 并使用shinyjs 根据单选按钮的值切换其中一个:

    library(shiny)
    library(shinyjs)
    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"),
      shinyjs::useShinyjs(),
      tabsetPanel(
        tabPanel('Data',  
                 
                 radioButtons("county_or_zip_select","Choose:",choices=c("County","ZipCode")),
                 selectInput("selectCounty", "Select county", selectize = FALSE,choices=unique(county_list)),
                 selectInput("selectZipcode", "Select zip code", selectize = FALSE,choices=unique(zip_list)),
                 DT::DTOutput('data')
        )
      ))
    
    server <- function(input, output, session) {
      
      observe( {
        shinyjs::toggle('selectCounty',condition = input$county_or_zip_select=='County')
        shinyjs::toggle('selectZipcode',condition = input$county_or_zip_select=='ZipCode')
      })
    
      getData <- reactive({
        if(input$county_or_zip_select=='County'){
          db[grep(input$selectCounty, db$County,ignore.case = T),]
        } else {
          db[grep(input$selectZipcode, db$ZipCode, ignore.case = T),]}
      })
      
      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)
    

    请注意,需要在 UI 中调用 shinyjs::useShinyjs(),以便 shinyjs 正常工作。

    【讨论】:

      【解决方案2】:

      另一种方法是

      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")),
                   uiOutput("crz"),
                   DT::DTOutput('data')
          )
        ))
      
      server <- function(input, output, session) {
        
        output$crz <- renderUI({
          if (input$county_or_zip_select == "County") {
            mychoices <- unique(c(county_list))
            mylabel <- "Select County: "
          }else {
            mychoices <- unique(c(zip_list))
            mylabel <- "Select Zip Code: "
          }
          selectInput("search4_all", mylabel, selectize = FALSE, choices=mychoices )
        })
        
        getData <- reactive({
          if(input$county_or_zip_select=='County'){
            df<-db[grep(input$search4_all, db$County,ignore.case = T),]
          }else{
            df<-db[grep(input$search4_all, db$ZipCode, ignore.case = T),]}
          
          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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 2021-12-14
        • 2017-02-03
        • 2015-05-10
        • 2021-11-19
        • 2021-10-17
        • 2023-01-23
        相关资源
        最近更新 更多