我第一次使用日期变量作为我正在整理的Shiny应用程序的过滤器,我无法理解为什么代码没有返回任何案例。我已经使用mydf
包预处理了lubridate
中的数据(此处只包含我遇到问题的变量)。我一直在尝试各种方式,包括as.Date
,as_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)
答案 0 :(得分:1)
我认为你的过滤条件是你想要的。并且,要在输出的闪亮表中显示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
})