Shiny GGPlot Error: Arguments must have the same length

I am trying to create a shiny app bar plot with dataframe columns (x variables) as input selections. One solution has been posted online already, but it has not worked for me.

My code is the following:

ui <- fluidPage(
  sidebarLayout(position = "left",
        sidebarPanel(
          selectInput('x', "Funding:", choices = c('source_location',
            'source_organizationtype','destination_purpose','destination_organizationtype'), 
             selected = NULL),
        theme = shinytheme("cerulean")),
        
        mainPanel(plotOutput("outplot"))
))

server <- function(input, output) {
  output$outplot <- renderPlot( {
    selected_data <- graph_funds_data %>% select(input$x, funding)
    ggplot(selected_data, aes(x= reorder( !! input$x, funding), y = funding, 
                             fill = !! input$x, 
                             color = !! input$x)) + 
      geom_bar(position="stack", stat= 'identity') + 
      theme_minimal() + labs(x = as.name(input$x), y = 'Funding in Billions (USD)',
                             title = 'Total Incoming Ukraine Crisis Funding', 
                             subtitle = 'January-April 2022') + 
      theme(legend.position="bottom", legend.title=element_blank(), 
            legend.direction="horizontal", legend.box="vertical")
   } )
  
}

shinyApp(ui = ui, server = server, options = list(height=1000)) 

And I get 'Error: arguments must have same length'

I have also tried:

ui <- fluidPage(
  sidebarLayout(position = "left",
        sidebarPanel(
          selectInput('x', "Funding:", choices = c('source_location',
            'source_organizationtype','destination_purpose','destination_organizationtype'), 
             selected = NULL),
        theme = shinytheme("cerulean")),
        
        mainPanel(plotOutput("outplot"))
))

server <- function(input, output) {
  output$outplot <- renderPlot( {
    selected_data <- graph_funds_data %>% select(input$x, funding)
    ggplot(selected_data(), aes(x= reorder(selected_data()[[input$x]], funding), y = funding, 
                             fill = selected_data()[[input$x]], 
                             color = selected_data()[[input$x]])) + 
      geom_bar(position="stack", stat= 'identity') + 
      theme_minimal() + labs(x = as.name(input$x), y = 'Funding in Billions (USD)',
                             title = 'Total Incoming Ukraine Crisis Funding', 
                             subtitle = 'January-April 2022') + 
      theme(legend.position="bottom", legend.title=element_blank(), 
            legend.direction="horizontal", legend.box="vertical")
   } )
  
}

shinyApp(ui = ui, server = server, options = list(height=1000)) 

And I get 'Error: Could not find function "selected_data"'

Does anyone know how to fix this?

Could you format this into a reproducible example? That is a set of code that folks can easily get up and running to replicate your issue? Currently, this is only part of a shiny app, as there is no example data provided. Also if there are elements not relevant, they might be removed from the reprex, i.e. 'shinytheme'

IF you aren't familiar with best practices for shiny reprexes, check out

This will make it easier for folks to replicate your issue and offer suggestions to solve it.

This topic was automatically closed 54 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.