Have multiple inputs going into multiple graphs but only 3 out of the 5 are working.

I have created multiple selectInputs that will alter multiple graphs when something from the drop down menu has been selected. Currently only 3 out of the 5 graphs are working even though they all have the same code.
UI:

ui <- dashboardPage(
  dashboardHeader(title = "Human Trafficking"),
  
  dashboardSidebar(
    sidebarMenu(
      selectInput("Source", "Choose a Data Source: ", choices = sort(unique(newNgo$Data.Source)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("Nationality", "Select a nation: ", choices = " "),
      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")
      #actionButton("button1", "Apply"),
      #actionButton("reset_input", "Reset inputs")
    )
  ),
fluidRow(     
      box(width = 6, solidHeader = TRUE, status = "primary",
          title = "Trafficking Type", 
          selectInput("traffickingType", "Choose a trafficking type: ", 
                      choices = sort(unique(newNgo$Trafficking.Type)), selected = NULL,
                      multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
          #actionButton("button2", "Apply"),
          plotlyOutput("coolplot", width = '750px', height = '300px')
      ),
      
      box(width = 6, solidHeader = TRUE, status = "primary",
          title = "Trafficking Sub-Type", 
          selectInput("traffickingSubType", "Choose a trafficking sub type: ",
                      choices = sort(unique(newNgo$Trafficking.Sub.Type)), selected = NULL,
                      multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
          #actionButton("button3", "Apply"),
          plotlyOutput("Sub", width = '750px', height = '300px')
      )
    ),
    
    fluidRow(
      box(width = 4, solidHeader = TRUE, status = "primary",
          title = "Victim Gender", 
          selectInput("victimGender", "Choose a gender: ", 
                      choices = sort(unique(newNgo$Victim.Gender)), selected = NULL,
                      multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
          #actionButton("button4", "Apply"),
          plotlyOutput("gender", width = '250px', height = '200px')
      ),
      
      box(width = 4, solidHeader = TRUE, status = "primary",
          title = "Transport Method",
          selectInput("transp", "Choose a transportation method: ", 
                      choices = sort(unique(newNgo$Transportation.Method)), selected = NULL,
                      multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
          #actionButton("button4", "Apply"),
          plotlyOutput("transportMethod", width = '250px', height = '200px')
      ),
      
      box(width = 4, solidHeader = TRUE, status = "primary",
          title = "Control Method",
          selectInput("control", "Choose a control method: ", 
                      choices = sort(unique(newNgo$Control.Method)), selected = NULL,
                      multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
          #actionButton("button4", "Apply"),
          plotlyOutput("controlMethod", width = '250px', height = '200px')
    )

Server:

output$coolplot <- renderPlotly({
     req(input$Nationality)
     
     if(!is.null(input$Nationality)) {
       newNgo <- newNgo %>% filter(Victim.Nationality %in% input$Nationality)
     }
     if(!is.null(input$gender)) {
       newNgo <- newNgo %>% filter(Victim.Gender %in% input$gender)
     }
     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)
     }
  
     plot_ly(newNgo, labels = ~Trafficking.Type, type = "pie") %>%
       layout(showlegend = FALSE)
  })
output$control <- renderPlotly({
     req(input$Nationality)

     if(!is.null(input$Nationality)) {
       newNgo <- newNgo %>% filter(Victim.Nationality %in% input$Nationality)
     }
     if(!is.null(input$gender)) {
       newNgo <- newNgo %>% filter(Victim.Gender %in% input$gender)
     }
     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)
     }

     plot_ly(newNgo, labels = ~Control.Method, type = "pie") %>%
       layout(showlegend = FALSE)
   })

I have attached the code from the server with one graph that works and one that doesn't work. I also attached all the different inputs I have from the UI.

Hi @fitzer23. Because you didn't show all script that you use, so please explain more about the problem. And from you script provided, can you explain why you need to do the same set of filter step to the same data table for a number of times. In my opinion, modifying the original data and assign to the same name is not a good practice. Every step that modifying the original data should assign to another name.

I think I'v fixed the problem but the reason I have the same filters is because I have 5 inputs and 5 graphs that are linked so if trafficking type is changed all the other graphs will change with whats related to the trafficking type selected etc. If you have a better way of doing it I'd be more than happy to try it out. Thanks

If you do the same set of filter steps to the same data table and plot graphs with it, you can do it once and store it like the reactive function. Plot all 5 graphs with the filtered data. It will make the code more clear and readable.

filteredData <- reactive({
  req(input$Nationality)
  filtNgo <- newNgo
  
  if(!is.null(input$Nationality)) {
    filtNgo <- filtNgo %>% filter(Victim.Nationality %in% input$Nationality)
  }
  if(!is.null(input$gender)) {
    filtNgo <- filtNgo %>% filter(Victim.Gender %in% input$gender)
  }
  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$coolplot <- renderPlotly({
  plot_ly(filteredData(), labels = ~Trafficking.Type, type = "pie") %>%
    layout(showlegend = FALSE)
})

output$control <- renderPlotly({
  plot_ly(filteredData(), labels = ~Control.Method, type = "pie") %>%
    layout(showlegend = FALSE)
})

Thanks makes it so much easier!!

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