无法正确过滤Shiny应用程序中的日期
问题描述:
我有我的第一次去使用日期变量作为一个Shiny应用程序的过滤器我放在一起,我无法理解为什么代码不返回任何情况。我使用lubridate
包预处理了mydf
(这里仅包含我遇到的变量)的数据。我一直在尝试各种方式,包括as.Date
,as_date
等,但没有成功。我错过了什么?下面无法正确过滤Shiny应用程序中的日期
代码:
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)
答
我觉得你有你的过滤条件向后从你想要什么。并且,要将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
})