why date range function keep throwing error no matter what I do

library(shiny)
library(plotly)
library(dplyr)
library(xts)
library(lubridate)
# re<- read.csv("C:/Users/dell/desktop/tdd.csv")
re<-data.frame(date=c("1/1/2020","1/11/2020", "1/13/2020", "2/23/2020", "2/12/2020","3/23/2020","4/20/2020"),names=c
               ("A1","B1","C1","D1","E1","F1","G1"),values=c(400,200,600,300,700,100,300))

ui <- fluidPage(
  titlePanel("Plotly - dateRangeInput"),
  sidebarLayout(
    sidebarPanel(
      dateRangeInput(inputId = "date",label = "Date",
                 start = Sys.Date() - 28, end = Sys.Date() + 2,
                 format = "mm/dd/yyyy",
                 separator = "TO")),
    mainPanel(
      fluidPage(plotlyOutput("p1"))
    )
  )
)

server<-function(input,output,session){
#re$date<-mdy(re$date)
output$p1<-renderPlotly({
  re$date<-as.Date(re$date,format = "%m/%d/%Y")
  class(re$date)

  p<-plot_ly(x=re$names,y=re$values,type="bar") %>% 
   # filter(re$date>= input$date[1] & re$date<= input$date[2])
   filter(re$date>= as.Date(input$date[1]) & re$date<= as.Date(input$date[2]))
  p
  
})
}
shinyApp(ui,server)

could anyone tell me what i am doing wrong here, date is kinda frustating error ,no what I do it keeps on throwing error, I have converetd into date object,tried lubradidate "mdy" function and tried input$date[1] & input$date[2], tried to convert into as.Date(input$date),defined it earlier with format syntax too but this still throws error.please someone let me know if they get what I am missing here.

you had two problems

  1. dplyr:: filter works on data.frames, not plotly objects. you piped a plotly object to filter, and you shouldnt.
  2. when using filter on a dataframe, when you reference columns in that dataframe you don't use $ syntax, because you have direct access.
    therefore your render function should look like
output$p1<-renderPlotly({
    re$date<-as.Date(re$date,format = "%m/%d/%Y")

    re_filt <- filter(re,
           between(date,
                   as.Date(input$date[1]) ,
                   as.Date(input$date[2])))
    
    p<-plot_ly(data = re_filt,
               x=~names,y=~values,type="bar") 
    p
    
  })

I ran this and put the initial date back to 2020 and chart contents appeared.

I don't know what I am doing wrong here ,I did whatever I could to make it work,and was looking for work around this issue but couldn't get it done,please help me to get it run by pointing out the mistake that i am commiting here.any help and suggestion would be great and i am kinda stuck to this issue for quite a long time and it is an essential part of the project that i am currently working on.I know it is some kind of syntax error ,I might have put the date filter on the wrong place ,but unable to figure out exact problem here.

library(shiny)
library(plotly)
library(dplyr)
library(readr)
library(shinydashboard)
library(shinythemes)
library(dashboardthemes)
library(shinyWidgets)
# total<-read.csv("c:/users/dell/desktop/ssss.csv")
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,
         "B-25612", "4/14/2017",   "Shrichand",            "Punjab",         "Chandigarh",   193L,
         "B-25613", "4/14/2017",      "Mukesh",           "Haryana",         "Chandigarh",   180L,
         "B-25614", "4/15/2017",     "Vandana",  "Himachal Pradesh",              "Simla",   116L,
         "B-25615", "4/15/2017",      "Bhavna",            "Sikkim",            "Gangtok",   107L,
         "B-25616", "4/16/2017",       "Kanak",               "Goa",                "Goa",    12L,
         "B-25617", "4/16/2017",       "Sagar",          "Nagaland",             "Kohima",    38L,
         "B-25618", "4/17/2017",       "Manju",    "Andhra Pradesh",          "Hyderabad",    65L,
         "B-25619", "4/17/2017",      "Ramesh",           "Gujarat",          "Ahmedabad",   157L,
         "B-25620", "4/18/2017",      "Sarita",       "Maharashtra",               "Pune",    75L,
         "B-25621", "4/19/2017",      "Deepak",    "Madhya Pradesh",             "Bhopal",    87L,
         "B-25622", "4/20/2017",     "Monisha",         "Rajasthan",             "Jaipur",    50L,
         "B-25623", "4/21/2017",      "Atharv",       "West Bengal",            "Kolkata",  1364L,
         "B-25624", "4/22/2017",        "Vini",         "Karnataka",          "Bangalore",   476L,
         "B-25625", "4/22/2017",       "Pinky", "Jammu and Kashmir",            "Kashmir",   257L,
         "B-25626", "4/23/2017",      "Bhishm",       "Maharashtra",             "Mumbai",   856L,
         "B-25627", "4/23/2017",      "Hitika",    "Madhya Pradesh",             "Indore",   485L,
         "B-25628", "4/24/2017",       "Pooja",             "Bihar",              "Patna",    25L,
         "B-25629", "4/26/2017",      "Hemant",            "Kerala", "Thiruvananthapuram",  1076L,
         "B-25630", "4/27/2017",       "Sahil",            "Punjab",         "Chandigarh",   107L,
         "B-25631", "4/28/2017",        "Ritu",           "Haryana",         "Chandigarh",    68L,
         "B-25632", "4/28/2017",      "Manish",  "Himachal Pradesh",              "Simla",   781L,
         "B-25633", "4/29/2017",        "Amit",            "Sikkim",            "Gangtok",    43L,
         "B-25634", "4/29/2017",      "Sanjay",               "Goa",                "Goa",    30L,
         "B-25635", "4/30/2017",       "Nidhi",          "Nagaland",             "Kohima",   160L,
         "B-25636", "4/30/2017",       "Nishi",       "Maharashtra",             "Mumbai",   259L,
         "B-25637",  "5/1/2017",       "Ashmi",    "Madhya Pradesh",             "Indore",  1603L,
         "B-25638",  "5/1/2017",       "Parth",       "Maharashtra",               "Pune",   494L,
         "B-25639",  "5/2/2017",       "Lisha",    "Madhya Pradesh",             "Bhopal",    98L,
         "B-25640",  "5/3/2017",     "Paridhi",         "Rajasthan",             "Jaipur",    68L,
         "B-25641",  "5/4/2017",     "Parishi",       "West Bengal",            "Kolkata",    42L,
         "B-25642",  "5/5/2017",        "Ajay",         "Karnataka",          "Bangalore",   116L,
         "B-25643",  "5/6/2017",       "Kirti", "Jammu and Kashmir",            "Kashmir",    22L,
         "B-25644",  "5/6/2017",      "Mayank",       "Maharashtra",             "Mumbai",    14L,
         "B-25645",  "5/7/2017",      "Yaanvi",    "Madhya Pradesh",             "Indore",   305L,
         "B-25646",  "5/7/2017",       "Sonal",             "Bihar",              "Patna",   362L,
         "B-25647",  "5/8/2017",      "Sharda",            "Kerala", "Thiruvananthapuram",    12L,
         "B-25648",  "5/9/2017",      "Aditya",            "Punjab",         "Chandigarh",   353L,
         "B-25649", "5/10/2017",      "Rachna",           "Haryana",         "Chandigarh",   193L,
         "B-25650", "5/11/2017",      "Chirag",       "Maharashtra",             "Mumbai",   233L,
         "B-25651", "5/12/2017",      "Anurag",    "Madhya Pradesh",             "Indore",   228L,
         "B-25652", "5/13/2017",     "Tushina",               "Goa",                "Goa",   333L,
         "B-25653", "5/13/2017",       "Farah",          "Nagaland",             "Kohima",   534L,
         "B-25654", "5/14/2017",       "Sabah",       "Maharashtra",             "Mumbai",    53L,
         "B-25655", "5/14/2017",        "Nida",    "Madhya Pradesh",             "Indore",   158L,
         "B-25656", "5/16/2017",    "Priyanka",       "Maharashtra",               "Pune",   149L,
         "B-25657", "5/17/2017",      "Tulika",    "Madhya Pradesh",             "Bhopal",   105L,
         "B-25658", "5/19/2017",     "Shefali",         "Rajasthan",             "Jaipur",    26L,
         "B-25659", "5/21/2017",   "Sanskriti",       "West Bengal",            "Kolkata",    97L,
         "B-25660", "4/18/2008",      "Shruti",         "Karnataka",          "Bangalore",    59L,
         "B-25661", "4/19/2008",  "Subhashree", "Jammu and Kashmir",            "Kashmir",   635L,
         "B-25662", "4/19/2008",       "Sweta",       "Maharashtra",             "Mumbai",    46L,
         "B-25663", "4/20/2008",  "Pournamasi",    "Madhya Pradesh",             "Indore",  1103L,
         "B-25664", "4/20/2008", "Pratyusmita",             "Bihar",              "Patna",    55L,
         "B-25665", "4/21/2008",   "Chayanika",            "Kerala", "Thiruvananthapuram",    45L,
         "B-25666", "4/22/2008",       "Tanvi",            "Punjab",         "Chandigarh",    24L,
         "B-25667", "4/23/2008",      "Anjali",           "Haryana",         "Chandigarh",    35L,
         "B-25668", "4/24/2008",        "Rhea",  "Himachal Pradesh",              "Simla",  1560L,
         "B-25669", "4/25/2008",      "Piyali",            "Sikkim",            "Gangtok",   133L,
         "B-25670", "4/26/2008",     "Charika",               "Goa",                "Goa",   114L,
         "B-25671", "4/26/2008",      "Mitali",          "Nagaland",             "Kohima",   143L,
         "B-25672", "4/27/2008",    "Akanksha",    "Andhra Pradesh",          "Hyderabad",    40L,
         "B-25673", "4/27/2008",     "Arsheen",           "Gujarat",          "Ahmedabad",    34L,
         "B-25674", "4/28/2008",      "Mahima",       "Maharashtra",               "Pune",    42L,
         "B-25675", "4/29/2008",      "Shreya",    "Madhya Pradesh",             "Bhopal",    89L,
         "B-25676", "4/30/2008",     "Chandni",         "Rajasthan",             "Jaipur",    19L,
         "B-25677",  "5/1/2008",        "Ekta",       "West Bengal",            "Kolkata",   249L,
         "B-25678",  "5/1/2008",     "Bathina",         "Karnataka",          "Bangalore",   711L,
         "B-25679",  "5/2/2008",        "Avni",       "Maharashtra",             "Mumbai",   496L,
         "B-25680", "5/25/2008",     "Aayushi",    "Madhya Pradesh",             "Indore",   389L,
         "B-25681",  "5/3/2008",      "Bhawna",    "Madhya Pradesh",             "Indore",    40L,
         "B-25682",  "5/4/2008",     "Krutika",             "Bihar",              "Patna",    23L,
         "B-25683",  "5/4/2008",      "Shreya",            "Kerala", "Thiruvananthapuram",   382L,
         "B-25684",  "5/5/2008",    "Samiksha",       "Maharashtra",             "Mumbai",   637L,
         "B-25685",  "5/6/2008",     "Sheetal",    "Madhya Pradesh",             "Indore",   117L,
         "B-25686",  "5/7/2008",       "Pooja",  "Himachal Pradesh",              "Simla",   182L,
         "B-25687",  "5/8/2008",      "Sanjna",       "Maharashtra",             "Mumbai",   880L,
         "B-25688",  "5/8/2008",      "Swetha",    "Madhya Pradesh",             "Indore",   154L,
         "B-25689",  "5/9/2008", "Bhaggyasree",       "Maharashtra",             "Mumbai",   816L,
         "B-25690", "5/28/2008",      "Gunjan",    "Madhya Pradesh",             "Indore",  1629L,
         "B-25691", "5/10/2008",     "Akancha",       "Maharashtra",             "Mumbai",    68L,
         "B-25692", "5/11/2008",      "Rashmi",    "Madhya Pradesh",             "Indore",   314L,
         "B-25693", "5/11/2008",       "Parna",    "Madhya Pradesh",             "Bhopal",   122L,
         "B-25694", "5/12/2008",  "Subhasmita",         "Rajasthan",             "Jaipur",    22L,
         "B-25695", "5/13/2008",      "Suhani",       "West Bengal",            "Kolkata",   434L,
         "B-25696", "5/14/2008",      "Noopur",         "Karnataka",          "Bangalore",  1061L,
         "B-25697", "5/28/2008",       "Vijay", "Jammu and Kashmir",            "Kashmir",    50L,
         "B-25698", "5/15/2008",      "Amisha",        "Tamil Nadu",            "Chennai",    37L
         )

 
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"),
    selectInput(inputId = "p1",label="filter drop down",choices = c(unique(total$State)))
      )),
  dashboardBody(
    shinyDashboardThemes(
      theme = "onenote"
    ),
  fluidPage(
  plotlyOutput("state", height = 200),
  plotlyOutput("city", height = 200),
  plotlyOutput("customer", height = 200),
  plotlyOutput("sales", height = 300),
  dataTableOutput("datatable"))))

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

server <- function(input, output, session) {
  
  State <- reactiveVal()
  City <- reactiveVal()
  CustomerName<- reactiveVal()
  Order_Date <- reactiveVal()
  
  observeEvent(event_data("plotly_click", source = "State"), {
    State(event_data("plotly_click", source = "State")$x)
    City(NULL)
    Order_Date(NULL)
  })
  
 observeEvent(event_data("plotly_click", source = "City"), {
    City(event_data("plotly_click", source = "City")$x)
    CustomerName(NULL)
    Order_Date(NULL)
  })
 
  observeEvent(event_data("plotly_click", source = "CustomerName"), {
    CustomerName(event_data("plotly_click", source = "CustomerName")$x)
    Order_Date(NULL)
  })
  
  observeEvent(event_data("plotly_click", source = "Order_Date"), {
    Order_Date(event_data("plotly_click", source = "Order_Date")$x)
  })
  
  ## filter the data
  # filtered_data <- reactive({
  #   dplyr::filter(total, total$State == input$p1)
  # })
  
  output$state <- renderPlotly({
  
    total$Order_Date<-as.Date(total$Order_Date,format = "%m/%d/%Y")
     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") %>%
      axis_titles() %>% 
      layout(title = "State")
  })

   output$city <- renderPlotly({
    if (is.null(State())) return(NULL)
    total%>%
      filter( State %in% State()) %>%
      count(City, wt = sales) %>%
      plot_ly(x = ~City, y = ~n, source = "City") %>%
      axis_titles() %>%
      layout(title = State())
  })
 
  
  output$customer <- renderPlotly({
    if (is.null(City())) return(NULL)
    total%>%
      filter(City %in% City()) %>%
      count(CustomerName, wt = sales) %>%
      plot_ly(x = ~CustomerName, y = ~n, source = "CustomerName") %>%
      axis_titles() %>%
      layout(title = City())
  })
  
  output$sales <- renderPlotly({
    if (is.null(CustomerName())) return(NULL)
    
    total %>%
      filter(CustomerName %in% CustomerName()) %>%
      count(Order_Date, wt = sales) %>%
      plot_ly(x = ~Order_Date, y = ~n, source = "Order_Date") %>%
      add_lines() %>%
      axis_titles() %>%
      layout(title = paste(CustomerName(), "sales over time"))
  })
  
  output$datatable <- renderDataTable({
    if (is.null(Order_Date())) return(NULL)
    
    total %>%
      filter(
        CustomerName %in% CustomerName(),
        as.Date(Order_Date) %in% as.Date(Order_Date())
      )
  })
  
}
runApp(list(ui = ui, server = server), launch.browser = TRUE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.