How to add dates as an input into a reactive graph

I have added in dateInput to give the user the option of selecting between a date range which will then update the graph based on whether the date is within the range selected but I am not totally sure on how to get it into the server side of things?
UI dateInput

dateInput("startdate", "Start Date:", value = "2000-01-01", format = "dd-mm-yyyy",
                min = "2000-01-01", max = "2019-08-26"),
      
      dateInput("enddate", "End Date:", value = "2019-08-27", format = "dd-mm-yyyy",
                min = "2000-01-02", max = "2019-08-27")

Server:

observeEvent({input$button1 + input$button2 + input$button3 + input$button4},
               {
                 req(input$traffickingType)

                 if(!is.null(input$Nationality)) {
                   newNgo <- newNgo %>% filter(Victim.Nationality %in% input$Nationality)
                 }
                 if(!is.null(input$victimGender)) {
                   newNgo <- newNgo %>% filter(Victim.Gender %in% input$victimGender)
                 }
                 if(!is.null(input$traffickingType)) {
                   newNgo <- newNgo %>% filter(Trafficking.Type %in% input$traffickingType)
                 }
                 if(!is.null(input$traffickingSubType)) {
                   newNgo <- newNgo %>% filter(Trafficking.Sub.Type %in% input$traffickingSubType)
                 }
                 if(!is.null(input$Source)) {
                   newNgo <- newNgo %>% filter(Data.Source %in% input$Source)
                 }

                 output$coolplot <- renderPlotly(
                   {
                     plot_ly(newNgo, labels = ~Trafficking.Type, type = "pie") %>%
                       layout(showlegend = FALSE)
                   }
                 )
               })

As I have action buttons and multiple inputs into the graph, this is just one graph entry of 5, I am not sure how to get the data to read the date entered and factor it in!

Does your newNgo data have some sort of date column? Is there anyway you can share this dataset with us? If not, the result of str(newNgo) would help me point you in the right direction.

The data set has dates in it but from looking at the result of str(newNgo) it doesn't seem that they are down as a date. Here's the results:

'data.frame': 4489 obs. of 24 variables:
$ Data.Source : Factor w/ 1 level "ngo": 1 1 1 1 1 1 1 1 1 1 ...

$ Incident.Reporting.Date: int 20051105 20080209 20090524 20111123 20111123 20111123 20111127 20111220 20111227 20120103 ...

$ Country : Factor w/ 2 levels "","Nepal": 2 2 2 2 2 2 2 2 2 2 ...

$ City...Town : Factor w/ 1966 levels "","-","Aalapot",..: 623 710 1937 1833 1913 1913 1192 659 1003 1698 ...

$ Address...Location : logi NA NA NA NA NA NA ...

$ Trafficking.Type : Factor w/ 5 levels "","Domestic Servitude",..: 1 1 1 1 1 1 1 1 1 1 ...

$ Trafficking.Sub.Type : Factor w/ 8 levels "","Child Marriage",..: 1 1 1 1 1 1 1 1 1 1 ...

$ Sources : logi NA NA NA NA NA NA ...

$ ID.. : Factor w/ 4380 levels "000be3119b46af67f9769a7d8f943d0d23a9cfc6",..: 2726 2725 2735 407 406 408 943 3618 1783 3616 ...

$ Location.Type : Factor w/ 2 levels "Destination",..: 2 2 2 2 2 2 2 2 2 2 ...

$ Incident.Start.Date : Factor w/ 1585 levels "01/01/2013","01/01/2015",..: 705 89 1227 790 790 790 814 909 926 11 ...

$ Incident.End.Date : Factor w/ 1585 levels "01/01/2013","01/01/2015",..: 705 89 1227 790 790 790 814 909 926 11 ...

$ Recruitment.Method : Factor w/ 538 levels "","13 bonded labourers rescued in W Delhi",..: 1 230 404 480 480 480 1 1 1 480 ...

$ Control.Method : Factor w/ 6 levels "","{"error" : "Phrase too long",\n "description" : "The phrase is too long. Maximum total length is 2,048 "| truncated,..: 1 1 1 1 1 1 1 1 1 1 ...

$ Transportation.Method : Factor w/ 7 levels "","Aeroplane",..: 1 1 1 1 1 1 1 1 1 1 ...

$ Trafficker.Nationality : Factor w/ 5 levels "","Asian","British",..: 1 1 1 1 1 1 1 1 1 1 ...

$ Trafficker.Gender : Factor w/ 4 levels "","Female","Male",..: 3 3 3 1 1 1 1 1 1 1 ...

$ Trafficker.Age : Factor w/ 70 levels "","0","1","118",..: 51 23 69 1 1 1 1 1 1 1 ...

$ Victim.Nationality : Factor w/ 25 levels "","Asian","Bangladesh",..: 15 15 15 15 15 15 15 15 15 15 ...

$ Victim.Gender : Factor w/ 4 levels "","Female","Male",..: 2 2 3 4 4 4 4 4 4 4 ...

$ Victim.Age : Factor w/ 67 levels "","0","10","11",..: 19 12 31 9 18 16 18 1 19 1 ...

$ InfoSource : Factor w/ 3 levels "Anonymous Tip",..: 3 3 3 3 3 3 3 3 3 3 ...

$ Title : logi NA NA NA NA NA NA ...

$ Geoname : logi NA NA NA NA NA NA ...

Convert Incident.Start.Date and Incident.End.Date to date with as.Date(Incident.Start.Date, format = "%d/%m/%Y") and as.Date(Incident.End.Date, format = "%d/%m/%Y").

In your coolplot: newNgo %>% filter(Incident.Start.Date >= input$startdate, Incident.End.Date <= input$enddate) %>% plotly(labels = ...) etc.

2 Likes

Thanks for the help!!

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