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:
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:
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