首页 > 解决方案 > R: object of type 'closure' is not subsettable (Rshiny problem)

问题描述

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?

标签: rshinydplyrshiny-server

解决方案


推荐阅读