2017-09-12 61 views
0

我有我的第一次去使用日期變量作爲一個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) 

回答

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 
    }) 
相關問題