How to add a row filter for a categorical variable? [closed]

I would like to add a filtering condition into my shiny app. Currently it is using plotly, but I am not married to plotly if another tool works better.

My data has a category called ‘municipalities’. I want to apply a row filter to the data frame using the user interface, then have the plot re-render based on the filtered data frame.

My code is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("filter", "filter", unique(data.brazil$municipalities)),
selectInput("xcol", "X Variable", names(data.brazil)),
selectInput("ycol", "Y Variable", names(data.brazil)),
selectInput("color", "Color Variable", names(data.brazil)),
selectInput("size", "Size Variable", names(data.brazil))
),
mainPanel(
plotlyOutput("plot")
)
)
)
server <- function(input, output) {
x <- reactive({ data.brazil[, input$xcol] })
y <- reactive({ data.brazil[, input$ycol] })
color <- reactive({ data.brazil[, input$color] })
size <- reactive({ data.brazil[, input$size] })
data <- reactive({data.brazil[ data.brazil$municipalities == input$filter, ]}) #row filter
output$plot <- renderPlotly({
plot_ly(x = x(), y = y(), color = color(), size = size(), type = "scatter", mode = "markers")
})
}
shinyApp(ui = ui, server = server)
</code>
<code>ui <- fluidPage( sidebarLayout( sidebarPanel( selectInput("filter", "filter", unique(data.brazil$municipalities)), selectInput("xcol", "X Variable", names(data.brazil)), selectInput("ycol", "Y Variable", names(data.brazil)), selectInput("color", "Color Variable", names(data.brazil)), selectInput("size", "Size Variable", names(data.brazil)) ), mainPanel( plotlyOutput("plot") ) ) ) server <- function(input, output) { x <- reactive({ data.brazil[, input$xcol] }) y <- reactive({ data.brazil[, input$ycol] }) color <- reactive({ data.brazil[, input$color] }) size <- reactive({ data.brazil[, input$size] }) data <- reactive({data.brazil[ data.brazil$municipalities == input$filter, ]}) #row filter output$plot <- renderPlotly({ plot_ly(x = x(), y = y(), color = color(), size = size(), type = "scatter", mode = "markers") }) } shinyApp(ui = ui, server = server) </code>
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("filter", "filter", unique(data.brazil$municipalities)),
      selectInput("xcol", "X Variable", names(data.brazil)),
      selectInput("ycol", "Y Variable", names(data.brazil)),
      selectInput("color", "Color Variable", names(data.brazil)),
      selectInput("size", "Size Variable", names(data.brazil))
    ),
    mainPanel(
      plotlyOutput("plot")
    )
  )
)

server <- function(input, output) {
  x <- reactive({ data.brazil[, input$xcol] })
  y <- reactive({ data.brazil[, input$ycol] })
  color <- reactive({ data.brazil[, input$color] })
  size <- reactive({ data.brazil[, input$size] })

  data <- reactive({data.brazil[ data.brazil$municipalities == input$filter, ]}) #row filter

  output$plot <- renderPlotly({
    
    plot_ly(x = x(), y = y(), color = color(), size = size(), type = "scatter", mode = "markers") 
  })
}

shinyApp(ui = ui, server = server)

The shiny app works fine, however the filter doesn’t work. Here is an image of the app.

There are lots of data points because there are 400+ municipalities. Hence I’d like to focus on one municipality only.

Note, I can do this if I set the color variable to municipalities, but the resulting graph is very slow.

For reference, a much trimmed data set in csv:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>municipalities,years,PI,ave.alt,ave.slope,percent.water,TempMaxSumYear,TempMinSumYear,TempMaxWinYear,TempMinWinYear
água santa,2002,0,774.572815534,12.4101,17.6974026941,27.54875,16.795,17.8705,7.6047875
água santa,2003,0.0926640927,774.572815534,12.4101,17.6974026941,28.455,16.954375,20.506875,8.7186375
água santa,2004,0.0489130435,774.572815534,12.4101,17.6974026941,28.19375,16.979375,23.6803125,12.526625
água santa,2005,0.1236263736,774.572815534,12.4101,17.6974026941,28.684375,16.6275,17.1528125,9.0095625
água santa,2006,0.0859872611,774.572815534,12.4101,17.6974026941,27.026875,18.47875,19.859375,9.00713125
água santa,2007,0.0666666667,774.572815534,12.4101,17.6974026941,27.92875,17.823125,24.66375,13.015625
água santa,2008,0,774.572815534,12.4101,17.6974026941,25.36875,15.869375,18.3678125,8.41500625
água santa,2009,0,774.572815534,12.4101,17.6974026941,27.228125,16.215625,20.14,12.2789375
água santa,2010,0.0503144654,774.572815534,12.4101,17.6974026941,27.3875,17.72,21.178125,12.288125
agudo,2002,0.0947368421,407.712389381,24.5182,15.9689860196,30.561875,19.44625,19.090625,9.2718125
agudo,2003,0,407.712389381,24.5182,15.9689860196,31.141875,19.37375,20.894375,9.237
agudo,2004,0,407.712389381,24.5182,15.9689860196,30.441875,19.1575,23.96125,13.56625
agudo,2005,0.0333333333,407.712389381,24.5182,15.9689860196,30.324375,16.899375,16.7575,8.363125
agudo,2006,0.2162162162,407.712389381,24.5182,15.9689860196,28.274375,19.110625,19.2931875,8.6406875
agudo,2007,0,407.712389381,24.5182,15.9689860196,28.64125,19.149375,26.29375,15.708125
agudo,2008,0,407.712389381,24.5182,15.9689860196,26.83,17.038125,18.3729375,9.29475
agudo,2009,0,407.712389381,24.5182,15.9689860196,29.051875,18.15,21.09375,13.132625
agudo,2010,0.3888888889,407.712389381,24.5182,15.9689860196,29.844375,19.703125,20.36875,12.4310625
alegria,2003,0,381.962962963,15.002,18.2600987717,32.520625,19.91125,24.050625,10.2309375
alegria,2004,0,381.962962963,15.002,18.2600987717,32.4425,20.22625,26.29125,15.0344375
alegria,2005,0,381.962962963,15.002,18.2600987717,31.98375,18.731875,19.6,10.382625
alegria,2006,0,381.962962963,15.002,18.2600987717,31.325,20.259375,21.95875,9.7706875
alegria,2007,0,381.962962963,15.002,18.2600987717,30.994375,20.284375,29.060625,14.6423125
alegria,2008,0,381.962962963,15.002,18.2600987717,29.72375,18.8275,21.89075,10.3185625
alegria,2009,0,381.962962963,15.002,18.2600987717,31.02375,18.784375,23.510625,13.7511875
alegria,2010,0,381.962962963,15.002,18.2600987717,31.8,20.263125,24.079375,19.283125
</code>
<code>municipalities,years,PI,ave.alt,ave.slope,percent.water,TempMaxSumYear,TempMinSumYear,TempMaxWinYear,TempMinWinYear água santa,2002,0,774.572815534,12.4101,17.6974026941,27.54875,16.795,17.8705,7.6047875 água santa,2003,0.0926640927,774.572815534,12.4101,17.6974026941,28.455,16.954375,20.506875,8.7186375 água santa,2004,0.0489130435,774.572815534,12.4101,17.6974026941,28.19375,16.979375,23.6803125,12.526625 água santa,2005,0.1236263736,774.572815534,12.4101,17.6974026941,28.684375,16.6275,17.1528125,9.0095625 água santa,2006,0.0859872611,774.572815534,12.4101,17.6974026941,27.026875,18.47875,19.859375,9.00713125 água santa,2007,0.0666666667,774.572815534,12.4101,17.6974026941,27.92875,17.823125,24.66375,13.015625 água santa,2008,0,774.572815534,12.4101,17.6974026941,25.36875,15.869375,18.3678125,8.41500625 água santa,2009,0,774.572815534,12.4101,17.6974026941,27.228125,16.215625,20.14,12.2789375 água santa,2010,0.0503144654,774.572815534,12.4101,17.6974026941,27.3875,17.72,21.178125,12.288125 agudo,2002,0.0947368421,407.712389381,24.5182,15.9689860196,30.561875,19.44625,19.090625,9.2718125 agudo,2003,0,407.712389381,24.5182,15.9689860196,31.141875,19.37375,20.894375,9.237 agudo,2004,0,407.712389381,24.5182,15.9689860196,30.441875,19.1575,23.96125,13.56625 agudo,2005,0.0333333333,407.712389381,24.5182,15.9689860196,30.324375,16.899375,16.7575,8.363125 agudo,2006,0.2162162162,407.712389381,24.5182,15.9689860196,28.274375,19.110625,19.2931875,8.6406875 agudo,2007,0,407.712389381,24.5182,15.9689860196,28.64125,19.149375,26.29375,15.708125 agudo,2008,0,407.712389381,24.5182,15.9689860196,26.83,17.038125,18.3729375,9.29475 agudo,2009,0,407.712389381,24.5182,15.9689860196,29.051875,18.15,21.09375,13.132625 agudo,2010,0.3888888889,407.712389381,24.5182,15.9689860196,29.844375,19.703125,20.36875,12.4310625 alegria,2003,0,381.962962963,15.002,18.2600987717,32.520625,19.91125,24.050625,10.2309375 alegria,2004,0,381.962962963,15.002,18.2600987717,32.4425,20.22625,26.29125,15.0344375 alegria,2005,0,381.962962963,15.002,18.2600987717,31.98375,18.731875,19.6,10.382625 alegria,2006,0,381.962962963,15.002,18.2600987717,31.325,20.259375,21.95875,9.7706875 alegria,2007,0,381.962962963,15.002,18.2600987717,30.994375,20.284375,29.060625,14.6423125 alegria,2008,0,381.962962963,15.002,18.2600987717,29.72375,18.8275,21.89075,10.3185625 alegria,2009,0,381.962962963,15.002,18.2600987717,31.02375,18.784375,23.510625,13.7511875 alegria,2010,0,381.962962963,15.002,18.2600987717,31.8,20.263125,24.079375,19.283125 </code>
municipalities,years,PI,ave.alt,ave.slope,percent.water,TempMaxSumYear,TempMinSumYear,TempMaxWinYear,TempMinWinYear
água santa,2002,0,774.572815534,12.4101,17.6974026941,27.54875,16.795,17.8705,7.6047875
água santa,2003,0.0926640927,774.572815534,12.4101,17.6974026941,28.455,16.954375,20.506875,8.7186375
água santa,2004,0.0489130435,774.572815534,12.4101,17.6974026941,28.19375,16.979375,23.6803125,12.526625
água santa,2005,0.1236263736,774.572815534,12.4101,17.6974026941,28.684375,16.6275,17.1528125,9.0095625
água santa,2006,0.0859872611,774.572815534,12.4101,17.6974026941,27.026875,18.47875,19.859375,9.00713125
água santa,2007,0.0666666667,774.572815534,12.4101,17.6974026941,27.92875,17.823125,24.66375,13.015625
água santa,2008,0,774.572815534,12.4101,17.6974026941,25.36875,15.869375,18.3678125,8.41500625
água santa,2009,0,774.572815534,12.4101,17.6974026941,27.228125,16.215625,20.14,12.2789375
água santa,2010,0.0503144654,774.572815534,12.4101,17.6974026941,27.3875,17.72,21.178125,12.288125
agudo,2002,0.0947368421,407.712389381,24.5182,15.9689860196,30.561875,19.44625,19.090625,9.2718125
agudo,2003,0,407.712389381,24.5182,15.9689860196,31.141875,19.37375,20.894375,9.237
agudo,2004,0,407.712389381,24.5182,15.9689860196,30.441875,19.1575,23.96125,13.56625
agudo,2005,0.0333333333,407.712389381,24.5182,15.9689860196,30.324375,16.899375,16.7575,8.363125
agudo,2006,0.2162162162,407.712389381,24.5182,15.9689860196,28.274375,19.110625,19.2931875,8.6406875
agudo,2007,0,407.712389381,24.5182,15.9689860196,28.64125,19.149375,26.29375,15.708125
agudo,2008,0,407.712389381,24.5182,15.9689860196,26.83,17.038125,18.3729375,9.29475
agudo,2009,0,407.712389381,24.5182,15.9689860196,29.051875,18.15,21.09375,13.132625
agudo,2010,0.3888888889,407.712389381,24.5182,15.9689860196,29.844375,19.703125,20.36875,12.4310625
alegria,2003,0,381.962962963,15.002,18.2600987717,32.520625,19.91125,24.050625,10.2309375
alegria,2004,0,381.962962963,15.002,18.2600987717,32.4425,20.22625,26.29125,15.0344375
alegria,2005,0,381.962962963,15.002,18.2600987717,31.98375,18.731875,19.6,10.382625
alegria,2006,0,381.962962963,15.002,18.2600987717,31.325,20.259375,21.95875,9.7706875
alegria,2007,0,381.962962963,15.002,18.2600987717,30.994375,20.284375,29.060625,14.6423125
alegria,2008,0,381.962962963,15.002,18.2600987717,29.72375,18.8275,21.89075,10.3185625
alegria,2009,0,381.962962963,15.002,18.2600987717,31.02375,18.784375,23.510625,13.7511875
alegria,2010,0,381.962962963,15.002,18.2600987717,31.8,20.263125,24.079375,19.283125

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật