无法在 Shiny 应用程序中按日期正确过滤

Unable to correctly filter by dates in a Shiny app

我第一次使用日期变量作为我正在组装的 Shiny 应用程序的过滤器,我无法理解为什么代码 returns 没有案例。我已经使用 lubridate 包对 mydf 中的数据进行了预处理(此处仅包含我遇到问题的变量)。我一直在尝试各种方法,包括 as.Dateas_date 等,但都没有成功。我错过了什么?

代码如下:

library(shiny)
library(dplyr)

mydf <- structure(list(EndDate = structure(c(17345, 17344, 17343, 17341, 
                                 17341, 17340, 17340, 17339, 17339, 17339, 17339, 17339, 17339, 
                                 17338, 17338, 17338, 17338, 17338, 17338, 17338, 17338, 17338, 
                                 17338, 17338, 17338, 17338, 17338, 17338, 17338, 17338, 17337, 
                                 17337, 17337, 17337, 17337, 17337, 17337, 17337, 17337, 17337, 
                                 17336, 17336, 17336, 17336, 17335, 17335, 17335, 17335, 17335, 
                                 17335, 17335, 17335, 17334, 17334, 17334, 17334, 17334, 17334, 
                                 17334, 17334, 17334, 17333, 17333, 17333, 17333, 17333, 17333, 
                                 17333, 17333, 17333, 17333, 17333, 17333, 17333, 17333, 17333, 
                                 17333, 17333, 17333, 17333, 17333, 17333, 17333, 17333, 17333, 
                                 17333, 17333, 17333, 17333, 17333, 17333, 17333, 17332, 17332, 
                                 17332, 17332, 17332, 17332, 17332, 17331, 17331, 17331, 17331, 
                                 17331, 17331, 17331, 17331, 17331, 17330, 17330, 17330, 17330, 
                                 17330, 17330, 17330, 17330, 17330, 17330, 17330, 17330, 17330, 
                                 17330, 17330, 17330, 17330, 17330, 17330, 17330, 17330, 17330, 
                                 17330, 17330, 17330, 17330, 17330, 17330, 17330, 17324, 17322, 
                                 17318, 17338, 17335), class = "Date")), class = c("tbl_df", "tbl", 
                                                                                   "data.frame"), row.names = c(NA, -142L), .Names = "EndDate")

ui <- fluidPage(
  sliderInput("date", "Select dates",
          min = min(mydf$EndDate),
          max = max(mydf$EndDate),
          value = c(min(mydf$EndDate), max(mydf$EndDate))),
  tableOutput("filtered_data")
)

server <- function(input, output, session) {
  filt_data <- reactive({
    filter(mydf, input$date[1] >= EndDate, input$date[2] <= EndDate)
  })

  output$filtered_data <- renderTable({
filt_data()
  })
}

shinyApp(ui, server)

我认为您的过滤条件与您想要的相反。并且,要在输出的闪亮 table 中将 Enddate 显示为日期,一种解决方案是将日期转换为字符。见下文:

 filt_data <- reactive({
    results = filter(mydf, EndDate >= input$date[1] & EndDate <= input$date[2] )

    # To avoid displaying dates as integers in outputted table
    results$EndDate = as.character(results$EndDate) 
    results
  })