Trying to create a reactive graph that will take in multiple different inputs

I have added multiple select inputs to my shiny app in the sidebar and in the main body and want to create a graph that will change when any of those inputs have been selected or changed but I keep getting the error Warning: Error in : Result must have length 56127, not 0.
UI:

ui <- dashboardPage(
  dashboardHeader(title = "Human Trafficking"),
  
  dashboardSidebar(
  sidebarMenu(
    selectInput("Source", "Choose a Data Source: ", choices = " ", selected = NULL,
                multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
    dateInput("startdate", "Start Date:", value = "2009-01-01", format = "dd-mm-yyyy",
              min = "2009-01-01", max = "2019-08-26"),
    
    dateInput("enddate", "End Date:", value = "2019-08-27", format = "dd-mm-yyyy",
              min = "2009-01-02", max = "2019-08-27"),
    selectInput("Nationality", "Select a nation: ", choices = " "),
    actionButton("button", "Apply")
  )
),

  dashboardBody(
    
    fluidRow(
      box(width = 4, solidHeader = TRUE,
                selectInput("traffickingType", "Choose a trafficking type: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
                ),
      box(width = 4, solidHeader = TRUE,
                selectInput("traffickingSubType", "Choose a trafficking sub type: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
                ),
      box(width = 4, solidHeader = TRUE,
                selectInput("gender", "Choose a gender: ", choices = " ", selected = NULL,
                            multiple = TRUE, selectize = TRUE, width = NULL, size = NULL)
          )
    )
)

Server:

server <- function(input, output, session) {
  
  genderVic = sort(unique(ngo$Victim.Gender))
  updateSelectInput(session, "gender", choices = genderVic)
  
  traffickingSub = sort(unique(ngo$Trafficking.Sub.Type))
  updateSelectInput(session, "traffickingSubType", choices = traffickingSub)
  
  trafficking = sort(unique(ngo$Trafficking.Type))
  updateSelectInput(session, "traffickingType", choices = trafficking)
  
  traffickerNationalities = sort(unique(ngo$Trafficker.Nationality))
  updateSelectInput(session, "TraffickerNation", choices = traffickerNationalities)
  
  dataSource = sort(unique(ngo$Data.Provided.By))
  updateSelectInput(session, "Source", choices = dataSource)
  
  nationalities = sort(unique(ngo$Victim.Nationality))
  updateSelectInput(session, "Nationality", choices = nationalities)
  
  output$coolplot <- renderPlotly({
    ngo <-
      ngo %>%
      filter(Victim.Nationality == input$Nationality,
             Victim.Gender == input$gender,
             Trafficking.Type == input$traffickingType
      )
    
    p = ggplot(ngo, aes(x = Victim.Age, fill = Trafficking.Type)) + 
      geom_bar(position = "stack")
    ggplotly(p) %>%
      layout(showlegend = FALSE)
  })
}

So currently only have it calling three of the inputs to test it but still getting an error.

Hi @fitzer23,

The error very much seems related to one of the filters which should be of length 56127 (the length of the data I suppose) but in fact is NULL or somehow ends up with a length of 0.

I see a few issues with this app as it is. Firstly, when you allow multiple inputs, then the == comparison is ambiguous, I think you should use %in%. Secondly when a plot or any other reactive output depends on "truthy" values of one or more inputs, I would always recommend using req() just immediately after the render*({ line.

Also, instead of initalizing selectors and then updating them, consider using renderUI on the server side and uiOutput on the ui side.

@valeri are you sure you need renderUI() here? The valid inputs are determined by the ngo dataset does not appear to change over the course of the session. I think you could do selectInput("traffickingType", ..., choices = sort(unique(ngo$Trafficking.Type)).

2 Likes

Indeed even better and more concise :slight_smile:
Thanks

I have it running now but the only way for the graph to show is by selecting something from all the inputs and making sure that it is all in the dataset. Is there a way I can change it so it will update if only one input is selected and keep updating the more or less selected. Also if its lank in the dataset it just won't effect the graph but won't make it disappear?
Thanks

I am sorry, but I don't quite understand what you are trying to achieve. Could you try providing a simple example app that we can run locally and doesn't depend on your specific data? If the graph depends on all the inputs (in your example it depends on only 3 of them) then logically it will only work if all inputs have valid (truthy) values.

So at the moment it depends on all the inputs, how do I get it so it is not dependent on any. For example if the user selects one drop down the graph will update and not need any of the other inputs but if the user adds a second input the graph will update with the 2nd input but not need the 3rd unless it is selected. Also if what the user has selected is null it won't change the graph.

@fitzer23 You will greatly increase your chances of a helpful reply if you provide a minimal reprex, or reproducible example, that lays out the basic structure of your problem so that people can see what you've already attempted and experiment with real code. You can learn more about how to create a reprex for shiny apps at https://mastering-shiny.org/introduction.html#reprex

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