首页 > 解决方案 > 如何从下拉列表中过滤数据?

问题描述

我想通过下拉列表过滤我的图表上的状态。例如,我有密度图和状态列表下拉列表,当我更改状态图时,应该将其更改为开启状态。但是,当我更改状态时,我的代码不会发生任何事情。

我的代码:

服务器

  output$overat <- renderPlot({
   filtered <-
   Medi_sum_small %>%
   filter(State == input$e1)
  ggplot(Medi_sum_small, aes(Hospital_Ownership)) +
    geom_density(aes(fill=factor(Hospital_overall_rating)), alpha=0.7) + 
    labs(x="Ownership",
         fill="Overall rating") +
    scale_x_discrete(labels = function(x) str_wrap(x,width=0.3))
  
  })

用户界面

box(
              title = "Select State"
              ,width = 3
              ,solidHeader = TRUE
              ,status = "primary"
              ,selectInput(
                'e1', 'State',
                c("All",unique(Medi_sum_small$State))
              )

Graphs should be changed when I change the stage.

标签: rggplot2shinyshinydashboardggplotly

解决方案


现在我可以做到了,我将代码更改为:

 output$overat <- renderPlot({
    ggplot(filtered <-
             Medi_sum_small %>%
             filter(State == input$e1), 
           aes(Hospital_Ownership)) +
      geom_density(aes(fill=factor(Hospital_overall_rating)), alpha=0.7) + 
      labs(x="Ownership",
           fill="Overall rating") +
      scale_x_discrete(labels = function(x) str_wrap(x,width=0.3))

  })

太感谢了


推荐阅读