1

library('shiny')
library('ggplot2')
library('dplyr')

data = read.csv('recent_grads.csv')
data = na.omit(data)

ui = fluidPage(
  titlePanel('The Economic Guide to Picking a College Major'),
  sidebarLayout(
    sidebarPanel(
      helpText('See the employment situation'),
      selectInput(
        inputId = 'yvar',
        label = 'Metrics',
        choices = c('Total graduates'='Total',
                    'Women rate'='ShareWomen',
                    'Unemploymentrate'='Unemployment_rate',
                    'Full time rate'='Full_time_rate',
                    'College jobs rate'='College_jobs_rate',
                    'Low wage jobs rate'='Low_wage_jobs_rate',
                    'Salary median'='Median',
                    'Salary P25th'='P25th',
                    'Salary P75th'='P75th')
      )
      ),
    mainPanel(
      plotOutput(outputId = 'plot1')
    )
    )
  )

server = function(input, output){
  top10 = reactive({
    head((data %>%
            arrange(desc(input$yvar))),10)
  }) 
  output$plot1 = renderPlot({
    ggplot(top10, aes_string(x=top10$Major,y=input$yvar)) +
      geom_col(aes(fill=top10$Major_category)) +
      ggtitle(input$yvar) +
      xlab('Major') + ylab('') + scale_fill_discrete(name='Major category')
  })
}
shinyApp(ui,server)

I tried to filter a big dataset, finding the top 10 records according to the metrics selected. However, it does not work. It says, 'Warning: Error in $: object of type 'closure' is not subsettable.'

If I remove codes about top10 and plot the whole dataset, it works.

Plotting the whole dataset is ugly, unacceptable....

What should I do?

  • 1
    you need to use `ggplot(top10(), ...)` – Shree Oct 23 '18 at 01:36
  • also possible to use `top_n(data, 10, Median)` to get `top10`. Much more readable. – Shree Oct 23 '18 at 01:40
  • You might get a more informative error message if you didn't use `data` as an object name. Likewise do not use `df`. They are both function names. – IRTFM Oct 23 '18 at 03:41
  • 1
    Possible duplicate of [What is “object of type ‘closure’ is not subsettable” error in Shiny?](https://stackoverflow.com/questions/40623749/what-is-object-of-type-closure-is-not-subsettable-error-in-shiny) – John Paul May 23 '19 at 20:36
  • You're missing the parentheses on `top10()` – divibisan May 24 '19 at 21:57

0 Answers0