输入无效:date_trans仅适用于Date类的对象

时间:2017-04-07 15:17:56

标签: r ggplot2 shiny

使用ggplot2实现此功能并通过为日期范围设置两个输入选项并使绘图调整x(日期值)相应调整,但是,我在下面收到此错误:

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)

1 个答案:

答案 0 :(得分: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)