2017-04-07 76 views
0

與GGPLOT2和閃亮實現這個類Date對象所進行的工作的日期範圍兩個輸入選擇,並有情節調節X(日期值)相應地調整,但是,我在下面得到這個錯誤:無效輸入:date_trans只有

Invalid input: date_trans works with objects of class Date only 

我該如何解決這個問題?這裏是我的代碼如下:

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



data <- read.csv("C:/Users/user/Documents/R/R-3.3.1/R CODE/MyData2.csv", header = TRUE) # reading .csv 
newdata <- as.data.frame(data) # converting csv to DF 
newdata$event_date <- as.factor(newdata$event_date) 

str(newdata) 

ui <- fluidPage(

    selectInput("select", label = h3("Select Model"), 
       choices = list("Toyota Camry", "Nissan Altima")), 

    dateInput("strtdate", "Select start date:"), 

    dateInput("enddate", "Select end date:"), 

    plotOutput("hist") 
) 

server <- function(input, output) { 

    output$hist <- renderPlot({ 

    df <- newdata %>% select(event_date,PlatformName,total_events) %>% filter(PlatformName == input$select) %>% group_by(event_date, PlatformName) %>% summarize(total_events = sum(total_events)) 

    gg <- ggplot(df, aes(x= event_date, y = total_events, group =PlatformName)) + 
     geom_line(aes(color = PlatformName)) + 


     labs(title="Total Events vs. Time", x="Event Dates", y="Total Events") + 

       aes(xmin = input$strtdate, 
        xmax = input$enddate) + 


     theme(plot.title=element_text(size=20, face="bold", hjust = 0.5), 
      axis.text.x=element_text(size=10, angle=90,hjust=1,vjust=0.5), 
      axis.text.y=element_text(size=5), 
      axis.title.x=element_text(size=10), 
      axis.title.y=element_text(size=10)) 

    print(gg) 

    }) 
} 

shinyApp(ui = ui, server = server) 
+0

尋求幫助時,你應該包括[重複的例子(http://stackoverflow.com/questions/5963269/how -to-使-A-大-R再現的-例如用樣本的輸入數據)。如果你在你的數據日期值,確保它們解析爲日期(無論他們有'級()''Date'或'POSIXct'或東西)。現在,你似乎使他們的因素而這不會使事情更容易對R理解。 – MrFlick

回答

0

解決了這個問題。只需要以日期格式轉換特定日期列即可。

途經:

newdata$event_date <- as.Date(newdata$event_date) 

因此,新的代碼:

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



data <- read.csv("C:/Users/user/Documents/R/R-3.3.1/R CODE/MyData2.csv", header = TRUE) # reading .csv 
newdata <- as.data.frame(data) # converting csv to DF 


newdata$event_date <- as.factor(newdata$event_date) #CODE IMPLEMENTAITON 


str(newdata) 

ui <- fluidPage(

    selectInput("select", label = h3("Select Model"), 
       choices = list("Toyota Camry", "Nissan Altima")), 

    dateInput("strtdate", "Select start date:"), 

    dateInput("enddate", "Select end date:"), 

    plotOutput("hist") 
) 

server <- function(input, output) { 

    output$hist <- renderPlot({ 

    df <- newdata %>% select(event_date,PlatformName,total_events) %>% filter(PlatformName == input$select) %>% group_by(event_date, PlatformName) %>% summarize(total_events = sum(total_events)) 

    gg <- ggplot(df, aes(x= event_date, y = total_events, group =PlatformName)) + 
     geom_line(aes(color = PlatformName)) + 


     labs(title="Total Events vs. Time", x="Event Dates", y="Total Events") + 

       aes(xmin = input$strtdate, 
        xmax = input$enddate) + 


     theme(plot.title=element_text(size=20, face="bold", hjust = 0.5), 
      axis.text.x=element_text(size=10, angle=90,hjust=1,vjust=0.5), 
      axis.text.y=element_text(size=5), 
      axis.title.x=element_text(size=10), 
      axis.title.y=element_text(size=10)) 

    print(gg) 

    }) 
} 

shinyApp(ui = ui, server = server)