用于筛选光泽表的Null SelectInput

问题描述 投票:0回答:1

我遇到了一个问题,我想使用从SelectInput输入的值来过滤DT表。这很好用,但是只有在我完成所有要过滤的值时,表格才会出现。

我希望该表在SelectInput中的值为NULL时出现,但是,这不起作用。我想知道是否有人对所有类别或仅某些类别的SelectInput在NULL中显示一些建议。换句话说,如果没有选择任何内容,我想关闭SelectInput筛选。

以下是与此问题相关的代码节选。

box(selectInput("category", "Select Category", sources_cat, selected = NULL, multiple = TRUE),
                                      selectInput("age", "Select Age Group", sources_age, selected = NULL, multiple = TRUE),
                                      selectInput("country", "Select Country", sources_count, selected = NULL, multiple = TRUE)),
                                  box(HTML("<br/> <h5><b>Topic Definitions</b></h5> <br/>")),
                                  ),
                              box(width = NULL, dataTableOutput('table'))
                      ),

server <- function(input, output){

output$table <- DT::renderDataTable({

    cat <- input$category
    country_input <- input$country
    age_select <- input$age
    input1 <- input1
    input2 <- input2



    fsources <- sources %>% 
        filter(grepl(country_input[1], country) | grepl(country_input[2], country) | grepl(country_input[3], country) |
               grepl(country_input[4], country))

    fsources <- fsources %>% 
        filter(grepl(age_select[1], age) | grepl(age_select[2], age) | grepl(age_select[3], age))

    fsources <- fsources %>% 
        filter(grepl(cat[1], topics) | grepl(cat[2], topics) | grepl(cat[3], topics) |
                   grepl(cat[4], topics))

    #this output table only appears if there is a country and a topic selected. If not, it has an error. One must chose a country or a topic
    datatable(fsources, extensions = 'Buttons', options = list(
        dom = 'Bfrtip',
        buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
    ))


})

SelectInput == NULL时的错误消息] >>

“

我遇到了一个问题,我想使用从SelectInput输入的值来过滤DT表。这很好用,但是仅当我完成了所有我自己的值时,表格才会出现...

r datatable shiny dplyr dt
1个回答
0
投票

将每个过滤器放在if语句中,例如

if (all(!is.null(country_input)) {
  fsources <- sources %>% 
                filter(
                  grepl(country_input[1], country) | 
                  grepl(country_input[2], country) | 
                  grepl(country_input[3], country) |
                  grepl(country_input[4], country)
  )
}
© www.soinside.com 2019 - 2024. All rights reserved.