R 无法在应用程序中按日期正确筛选
我第一次尝试使用日期变量作为我正在组装的闪亮应用程序的过滤器,我很难理解为什么代码不返回任何案例。我已经使用R 无法在应用程序中按日期正确筛选,r,shiny,lubridate,R,Shiny,Lubridate,我第一次尝试使用日期变量作为我正在组装的闪亮应用程序的过滤器,我很难理解为什么代码不返回任何案例。我已经使用lubridate包预处理了mydf中的数据(这里只是由我遇到问题的变量组成)。我一直在尝试各种方法,包括as.Date,as_Date,等等,但都没有成功。我错过了什么 代码如下: library(shiny) library(dplyr) mydf <- structure(list(EndDate = structure(c(17345, 17344, 17343, 1734
lubridate
包预处理了mydf
中的数据(这里只是由我遇到问题的变量组成)。我一直在尝试各种方法,包括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)
库(闪亮)
图书馆(dplyr)
mydf我认为你的过滤条件与你想要的相反。而且,要在输出的闪亮表格中将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
})
filt_data=input$date[1]&EndDate