r - Invalid input: date_trans works with objects of class Date only -
implementing ggplot2 , shiny having 2 input selections date ranges , have plot adjust x(date values) adjust accordingly, however, error below:
invalid input: date_trans works objects of class date
how can resolve this? here code below:
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 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)
solved issue. needed convert specific date column in date format.
via:
newdata$event_date <- as.date(newdata$event_date)
thus, new code:
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 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)
Comments
Post a Comment