Adding dates to a reactive graph

I previously asked this question before here : How to add dates as an input into a reactive graph but have since changed my filtering method and now the dates cause an error for the graphs. How do I go about getting all the graphs to change based on the dates selected.
UI:

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

Server:

server <- function(input, output, session) {
filteredData <- reactive({
    filtNgo <- newngo
if(!is.null(input$victimGender)) {
      filtNgo <- filtNgo %>% filter(Victim.Gender %in% input$victimGender)
    }
    if(!is.null(input$traffickingType)) {
      filtNgo <- filtNgo %>% filter(Trafficking.Type %in% input$traffickingType)
    }
    if(!is.null(input$traffickingSubType)) {
      filtNgo <- filtNgo %>% filter(Trafficking.Sub.Type %in% input$traffickingSubType)
    }
    if(!is.null(input$Source)) {
      filtNgo <- filtNgo %>% filter(Data.Source %in% input$Source)
    }
filtNgo
  })

output$traff <- renderPlotly({
    plot_ly(filteredData(), labels = ~Trafficking.Type, type = "pie",
            marker = list(colors = colors,
                          line = list(color = '#FFFFFF', width = 1))) %>%
      layout(showlegend = FALSE)
  })
  
  output$Sub <- renderPlotly({
    ggplot(filteredData(), aes(x=Trafficking.Sub.Type)) + geom_bar()
  })
  
  output$vGender <- renderPlotly({
    ggplot(filteredData(), aes(x=Victim.Gender)) + geom_bar()
  })

I have attached a few of the graphs ensure that more than one graph will work.

As I've mentioned in the past, you will be much more likely to get help if you create a reprex as described in http://mastering-shiny.org/action-workflow.html#reprex. As well as making it easier for people help you, creating reprexes will also help you build up your own Shiny skills.

I have created a reprex to test it out, if anything else needs to be added please let me know.

library(shinyjs)
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(shinyWidgets)

tType = c("Forced Labour", "Forced Labour", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation")
start = c("11/05/2005", "02/09/2008","5/24/2009","11/23/2011", "11/23/2011", "11/23/2011")
end = c("11/05/2005", "02/09/2008","5/24/2009","11/23/2011", "11/23/2011", "11/23/2011")

newngo = c(tType, start, end)
newngo = as.data.frame(newngo)

UI:

ui <- dashboardPage(skin = ("black"),
                    dashboardHeader(title = "Human Trafficking"),
                    
                    dashboardSidebar(
                      sidebarMenu(
                        
                        dateInput("start", "Start Date:", value = "2019-08-01", format = "dd-mm-yyyy",
                                  min = "2000-01-01", max = "2019-09-04"),
                        
                        dateInput("end", "End Date:", value = "2019-09-05", format = "dd-mm-yyyy",
                                  min = "2000-01-02", max = "2019-09-05")
                      )
                    ),
                    
                    dashboardBody(
                      fluidRow(     
                        box(width = 6, solidHeader = TRUE, status = "success",
                            title = "Trafficking Type", 
                            selectInput("traffickingType", "Choose a trafficking type: ", 
                                        choices = sort(unique(newngo$tType)), selected = NULL,
                                        multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
                            plotlyOutput("traff", width = '750px', height = '300px')
                        )
                        )
)
)

Server:

server <- function(input, output, session) {
  filteredData <- reactive({
    filtNgo <- newngo
    
    if(!is.null(input$traffickingType)) {
      filtNgo <- filtNgo %>% filter(tType %in% input$traffickingType)
    }
    
    filtNgo
  })
  
  output$traff <- renderPlotly({
    #newngo %>% filter(start >= input$startdate, end <= input$end) %>%
      plot_ly(filteredData(), labels = tType, type = "pie",
              marker = list(colors = colors,
                            line = list(color = '#FFFFFF', width = 1))) %>%
      layout(showlegend = FALSE)
  })
  
}

shinyApp(ui, server)

This is a good start, but it's not a reprex because I can't easily copy and paste it into my R session. You also seem to have a lot of code that is unrelated to the problem you're experiencing; I'd recommend removing that so it's easier to see exactly what the problem is.

1 Like

I have edited my answer above and think I have got it right this time. Thanks for the help too

A minimal reprex would look more like this:

library(shiny)
library(plotly)

newngo <- data.frame(
  tType = c("Forced Labour", "Forced Labour", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation"),
  start =  c("11/05/2005", "02/09/2008", "5/24/2009", "11/23/2011", "11/23/2011", "11/23/2011"),
  end = c("11/05/2005", "02/09/2008", "5/24/2009", "11/23/2011", "11/23/2011", "11/23/2011")
)

ui <- fluidPage(
  dateInput("start", "Start Date:",
    value = "2019-08-01", format = "dd-mm-yyyy",
    min = "2000-01-01", max = "2019-09-04"
  ),
  dateInput("end", "End Date:",
    value = "2019-09-05", format = "dd-mm-yyyy",
    min = "2000-01-02", max = "2019-09-05"
  ),
  plotlyOutput("traff", width = "750px", height = "300px")
)

server <- function(input, output, session) {
  output$traff <- renderPlotly({
    # newngo %>% filter(start >= input$startdate, end <= input$end) %>%
    plot_ly(newngo, color = ~tType, type = "pie")
  })
}

shinyApp(ui, server)

However, that doesn't work at all, and I don't know enough about plotly to diagnose the problem.

You have to pass numeric values for the pie chart, you just have to count the categories first

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

newngo <- data.frame(stringsAsFactors = FALSE,
  tType = c("Forced Labour", "Forced Labour", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation", "Sexual Exploitation"),
  start =  c("11/05/2005", "02/09/2008", "5/24/2009", "11/23/2011", "11/23/2011", "11/23/2011"),
  end = c("11/05/2005", "02/09/2008", "5/24/2009", "11/23/2011", "11/23/2011", "11/23/2011")
)

ui <- fluidPage(
  dateInput("start", "Start Date:",
            value = "2005-05-01", format = "dd-mm-yyyy",
            min = "2000-01-01", max = "2019-09-04"
  ),
  dateInput("end", "End Date:",
            value = "2019-09-05", format = "dd-mm-yyyy",
            min = "2000-01-02", max = "2019-09-05"
  ),
  plotlyOutput("traff", width = "750px", height = "300px")
)

server <- function(input, output, session) {
  output$traff <- renderPlotly({
    newngo %>%
      mutate_at(c("start", "end"), ~mdy(.)) %>% 
      filter(start >= ymd(input$start), end <= ymd(input$end)) %>%
      count(tType) %>% 
      plot_ly(values = ~n, labels = ~tType, type = "pie")
  })
}

shinyApp(ui, server)

@andresrcs Thanks for the help, that works just one small problem. When I run the code some of the data doesn't go through and I get the error Warning: 35 failed to parse. What would be the best way to deal with this and get the data to not fail. Thanks

I can't know without seeing sample data that produces this warning message.

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