为什么我在 R Shiny 中的 observeEvent() 调用在它们内部有反应表达式后什么都不做?

Why do my observeEvent() calls in R Shiny not do anything once they have reactive expressions inside of them?

我正在构建一个闪亮的应用程序,用户可以在其中过滤掉某些项目。我希望只有在特定日期范围内出现的项目名称才会出现在下拉列表中。

我已经能够填充 selectize 菜单并且已经能够做到这样用户就可以 select 全部或删除所有项目(从 到我之前问过的问题)。但是,现在我正试图让这些名称对日期做出反应,我之前问题中的 observeEvent 代码崩溃了。我试图将其包装在反应式表达式中,但没有任何反应。

如何使我的项目可以按日期过滤,同时仍然保留 select 全部并删除所有功能?

library(shiny)
library(plotly)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets) 
library(dplyr)
library(htmltools)
library(lubridate)



ui = fluidPage(
    tabsetPanel(
        tabPanel("View 1", fluid = TRUE,
                 sidebarLayout(
                     sidebarPanel(
                         h4("Select Your Desired Filters"),
                         div(id = "inputs",
                             dateRangeInput(
                                 inputId = "date_filter",
                                 label = "Filter by Month and Year",
                                 start = today(),
                                 end = (today() + 90),
                                 min = "2021-04",
                                 max = NULL,
                                 format = "yyyy-mm",
                                 startview = "month",
                                 weekstart = 0,
                                 language = "en",
                                 separator = " to ",
                                 width = NULL,
                                 autoclose = TRUE
                             ),
                             br()),
                         h5("Include/Exclude Specific Projects"),
                         selectizeInput(inputId = "filter_by_project",
                                        label = "Filter by Project",
                                        choices = sort(unique(test$project)), 
                                        multiple = TRUE,
                                        selected = sort(unique(test$project))),
                         actionButton(inputId = "remove_all", 
                                      label = "Unselect All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B"),
                         actionButton(inputId = "add_all", 
                                      label = "Select All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
                     ),
                     mainPanel(
                     )
                 )
        )
    )
)
server = function(input, output, session) {
    
#Here's the dataset
    test <- tibble(project = c("Justin", "Corey","Sibley"),
                   date = ymd(c("2021-04-20", "2021-04-01", "2021-05-05")),
                   april_2021 = c(10, 100, 101),
                   may_2021 = c(1, 4, 7))
    
#I want users to be able to filter the list of projects by date, which should update the selectize options
    test <- reactive({
        test %>%
            dplyr::filter(date >= input$date_filter[1],
                          date <= input$date_filter[2])
    })
    
    
    observeEvent(input$remove_all, {reactive({
        updateSelectizeInput(session,"filter_by_project",choices=sort(unique(test()$project)), 
                             selected=NULL, options = list(placeholder="Please Select at Least One Project")
        )
    })
    })
    observeEvent(input$add_all, {reactive({
        updateSelectizeInput(session,"filter_by_project",choices=sort(unique(test()$project)), selected=sort(unique(test()$project)) )
    })
    })
    
}
shinyApp(ui = ui, server = server)

你有大毛病。首先是为您的输入 data.frame 和您的反应元素使用相同的名称。您将它们都称为 test,这会导致混淆您是在尝试使用 data.frame 还是反应对象。您应该使用不同的名称。第二个问题是您不需要为 observeEvents() 调用使用 reactive()。您只需要将您想要 运行 的代码放在一个块中。

解决这些问题后,您的服务器功能应该更像这样

server = function(input, output, session) {
  
  #Here's the dataset
  testdata <- tibble(project = c("Justin", "Corey","Sibley"),
                 date = ymd(c("2021-04-20", "2021-04-01", "2021-05-05")),
                 april_2021 = c(10, 100, 101),
                 may_2021 = c(1, 4, 7))
  
  #I want users to be able to filter the list of projects by date, which should update the selectize options
  test <- reactive({
    testdata %>%
      dplyr::filter(date >= input$date_filter[1],
                    date <= input$date_filter[2])
  })
  
  
  observeEvent(input$remove_all, {
    updateSelectizeInput(session,"filter_by_project", choices=sort(unique(test()$project)), 
                         selected=NULL, options = list(placeholder="Please Select at Least One Project")
    )
  })
  observeEvent(input$add_all, {
    updateSelectizeInput(session,"filter_by_project", choices=sort(unique(test()$project)), selected=sort(unique(test()$project)) )
  })
  
}