date filter is not working with or without lubridate in R

Viewed 100

I don't know why date filter is not working with or without lubridate. I have tried to achieve it with as.Date() function and even with lubridate but it is still not working,I have tried to get it done with data table and it is working quite well in that case,it is something wrong with plotly which is causing error here. I am not sure what i am missing here. please could anyone guide me through this issue.

library(shiny)
library(plotly)
library(dplyr)
library(shinydashboard)
library(lubridate)

total<-tibble::tribble(
         ~Order.ID, ~Order_Date, ~CustomerName,              ~State,                ~City, ~sales,
         "B-25601",  "4/5/2017",      "Bharat",           "Gujarat",          "Ahmedabad",  1275L,
         "B-25602",  "4/6/2017",       "Pearl",       "Maharashtra",               "Pune",    66L,
         "B-25603",  "4/7/2017",       "Jahan",    "Madhya Pradesh",             "Bhopal",     8L,
         "B-25604",  "4/8/2017",      "Divsha",         "Rajasthan",             "Jaipur",    80L,
         "B-25605",  "4/8/2017",     "Kasheen",       "West Bengal",            "Kolkata",   168L,
         "B-25606",  "4/9/2017",       "Hazel",         "Karnataka",          "Bangalore",   424L,
         "B-25607",  "4/9/2017",    "Sonakshi", "Jammu and Kashmir",            "Kashmir",  2617L,
         "B-25608", "4/10/2017",     "Aarushi",        "Tamil Nadu",            "Chennai",   561L,
         "B-25609", "4/11/2017",      "Jitesh",     "Uttar Pradesh",            "Lucknow",   119L,
         "B-25610", "4/12/2017",      "Yogesh",             "Bihar",              "Patna",  1355L,
         "B-25611", "4/13/2017",       "Anita",            "Kerala", "Thiruvananthapuram",    24L
         )

ui <- dashboardPage(
  dashboardHeader(title="dashboard"),
  dashboardSidebar(
    sidebarMenu(
      dateRangeInput(inputId = "date",
                 label = 'Date range',
                 start = Sys.Date() - 28, end = Sys.Date() + 2,
                 format = "mm/dd/yyyy",
                 separator = "TO")
      )),
  dashboardBody(
  fluidPage(
  plotlyOutput("state", height = 200))))

axis_titles <- . %>%
  layout(
    xaxis = list(title = ""),
    yaxis = list(title = "Sales"))

server <- function(input, output, session) {
  
  State <- reactiveVal()
  
  observeEvent(event_data("plotly_click", source = "State"), {
    State(event_data("plotly_click", source = "State")$x)
  })
  
  output$state <- renderPlotly({
    
     #total$Order_Date<-as.Date(total$Order_Date,format = "%m/%d/%Y")
     total$Order_Date<- mdy(total$Order_Date)
     #class(total$Order_Date)
     #  
     re_filt <- filter(total,
           between(total$Order_Date,
                   as.Date(input$date[1]) ,
                   as.Date(input$date[2])))
     
     total%>%
      count(State, wt = sales) %>%
      plot_ly(data=re_filt,x = ~State, y = ~n, source = "State") %>%
      #filter(total$Order_Date>= input$date[1] & total$Order_Date<= input$date[2]) %>% 
      axis_titles() %>% 
      layout(title = "State")
  })

}
runApp(list(ui = ui, server = server), launch.browser = TRUE)
1 Answers

The issue is that you are passing two dataframes to plot_ly, on the one hand you are passing total %>% count(State, wt = sales) to plot_ly and at the same time you pass re_filt to plot_ly via the data argument.

When fixing this your code runs fine. Try this:

output$state <- renderPlotly({
    total$Order_Date<- mdy(total$Order_Date)

    re_filt <- filter(total,
                      between(total$Order_Date,
                              as.Date(input$date[1]) ,
                              as.Date(input$date[2])))
    
    re_filt %>%
      count(State, wt = sales) %>%
      plot_ly(x = ~State, y = ~n, source = "State") %>%
      axis_titles() %>% 
      layout(title = "State")
  })

enter image description here

Related