2017-01-27 113 views
0

我正在嘗試使直方圖對dateInputRange函數作出反應。我有我的應用程序已經工作,只有一個年齡滑塊輸出到直方圖,還有一個表格下方的結果也。我將dateInputRange添加到了我的代碼中,並根據我想要過濾的源數據中的列(基於日期)過濾了輸出,並且應用程序仍會啓動,但直方圖不再繪製。 renderTable仍然對滑塊輸入作出反應,但對於直方圖,我只是得到一個空白的灰色圖。R Shiny dateInputRange函數中斷直方圖

當我啓動應用程序,控制檯給我這個消息:

「警告在==.default(C(」 2016" 年10月11日, 「2016年10月16日」,「11月22日/ 2016" , 「2016年11月21日」,: 更長對象長度不短對象長度的倍數」

代碼如下:

ui <- fluidPage(


    titlePanel("ED Admissions"), 


    sidebarLayout(
     sidebarPanel(
     sliderInput("AgeInput","Age",min = 3, max = 125, c(3,65)), 

     dateRangeInput("DateInput", "Date") 
     ), 


     mainPanel(
     plotOutput("AgePlot"), 
     br(), br(), 
     tableOutput("ClientTable") 


    ) 
    ) 
) 

# Define server logic required to draw a histogram 
server <- function(input, output) { 

    output$AgePlot <- renderPlot({ 
    filtered <- 
    edadmits %>% 
     ##filtering is by variables in the dataset 
     filter(Client_AGE >= input$AgeInput[1], 
      Client_AGE <= input$AgeInput[2], 
      Discharge_Claim_Start_Date == input$DateInput 
      ) 

    ggplot(filtered, aes(Client_AGE))+ 
     geom_histogram() 
    }) 

    output$ClientTable <- renderTable({ 
    filtered <- 
     edadmits %>% 

     filter(Client_AGE >= input$AgeInput[1], 
      Client_AGE <= input$AgeInput[2]) 

    filtered 
    }) 
} 


shinyApp(ui = ui, server = server) 

這裏是一個重複的例子:

library(shiny) 
library(ggplot2) 
library(dplyr) 

Name <- c("Person 1","Person 2","Person 3","Person 4","Person 5","Person 6","Person 7","Person 8", 
      "Person 9","Person 10","Person 11","Person 12", "Person 13","Person 14","Person 15", 
      "Person 16","Person 17","Person 18","Person 19","Person 20") 

Diagnosis <- sample(1:10, 20, replace=TRUE) 

Discharge.Date <- sample(seq(as.Date('2016/12/01'), as.Date('2016/12/31'), by="day"), 20) 

Age <- sample(1:125, 20) 

edadmits <- data.frame(Name, Diagnosis, Discharge.Date, Age) 


# Define UI for application that draws a histogram 
ui <- fluidPage(

    # Application title 
    titlePanel("ED Admissions, 12.1.16-12.31.16"), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
    sidebarPanel(
     sliderInput("AgeInput","Age",min = 3, max = 125, c(3,65)), 

     dateRangeInput("DateInput", "Date", start= "2016-12-01", end= "2016-12-31", 
        min= "2016-12-01", max = "2016-12-31") 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     plotOutput("AgePlot"), 
     br(), br(), 
     tableOutput("ClientTable") 


    ) 
) 
) 

# Define server logic required to draw a histogram 
server <- function(input, output) { 

    output$AgePlot <- renderPlot({ 
    filtered <- 
     edadmits %>% 
     ##filtering is by variables in the dataset 
     filter(Age >= input$AgeInput[1], 
      Age <= input$AgeInput[2], 
      Discharge.Date >= input$DateInput[1], 
      Discharge.Date <= input$DateInput[2] 
    ) 

    ggplot(filtered, aes(Age))+ 
     geom_histogram() 
    }) 

    output$ClientTable <- renderTable({ 
    filtered <- 
     edadmits %>% 
     ##filtering is by variables in the dataset 
     filter(Age >= input$AgeInput[1], 
      Age <= input$AgeInput[2], 
      Discharge.Date >= input$DateInput[1], 
      Discharge.Date <= input$DateInput[2]) 

    filtered 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

替換'Discharge_Claim_Start_Date ==輸入$ DateInput'用'Discharge_Claim_Start_Date> =輸入$ DateInput [1],Discharge_Claim_Start_Date <= $輸入DateInput [2]' – HubertL

+0

謝謝,但這似乎不起作用,情節仍然沒有渲染。 – Yallweh

+0

控制檯中的錯誤消息確實消失了您的建議更改,但應用程序的行爲相同。將工作在一個可重複的例子。 – Yallweh

回答

0

這可能來自日期問題,因此請嘗試使用strftime()重新格式化它們。還以簡化代碼,可以使用reactive這樣的:

server <- function(input, output) { 
    filtered_data <- reactive({ 
    edadmits %>% 
     filter(Age >= input$AgeInput[1], 
      Age <= input$AgeInput[2], 
      Discharge.Date >= input$DateInput[1], 
      Discharge.Date <= input$DateInput[2]) %>% 
     mutate(Discharge.Date=strftime(Discharge.Date, "%Y/%m/%d")) 
    }) 

    output$AgePlot <- renderPlot(ggplot(filtered_data(), aes(Age))+geom_histogram()) 

    output$ClientTable <- renderTable(filtered_data()) 
} 
+0

謝謝,它確實看起來像日期是罪魁禍首。他們正在閱讀作爲「字符」,雖然從我可以告訴那應該沒關係dateRangeInput。將考慮重新格式化,我很新,並且今天下午無法完成這項工作,但我認爲這超出了這個問題的範圍。 – Yallweh