Home > Back-end >  Shiny slide bar malfunctioning - sliderInput()
Shiny slide bar malfunctioning - sliderInput()

Time:10-25

I am very new on R Shiny, but I do have a question rerarding the sliderInput().

I am able to make the bar "work", but apparently it is not understanding the values min() and max() values correctly. Maybe I am missing something... Please note in the picture bellow that even though I am sliding the bar to don't show values higher than 0.1, I still have some polygons whit values higher than 0.1 appearing (image bellow).

Image here

Data, available on Google drive: https://drive.google.com/drive/folders/1qF9-8-e0Y6P5X_NUY5XI_AfMhESOJMN2?usp=sharing

Here it goes a reproducible example....

library(shiny)    # for shiny apps
library(RColorBrewer)
library(leaflet)  # renderLeaflet function
#library(spData)   # loads the world dataset 

SU        = sf::st_read(dsn = "LinktoDataHere") 
SU$lrmm_SU= round(SU$lrmm_SU, digits=3)
factpal <- colorFactor(brewer.pal(n = 4, name ="BuPu") , SU$lrmm_SU) 



ui = fluidPage(
  sliderInput(inputId = "life", "Mixed effects predictions for SU",
              min(SU$lrmm_SU, na.rm = TRUE), 
              max(SU$lrmm_SU, na.rm = TRUE), 
              value = range(SU$lrmm_SU, na.rm = TRUE),step=0.1),
  leafletOutput(outputId = "map", width = "50%")
)
server = function(input, output) {
  output$map = renderLeaflet({
    leaflet() %>% 
      addProviderTiles("Esri.WorldGrayCanvas", group = "Gray") %>%
      addPolygons(data = SU[SU$lrmm_SU < input$life, ],
                  stroke = TRUE, 
                  fillOpacity = 0.5, 
                  weight = 0.2,
                  opacity = 1,
                  color = "gray",
                  dashArray = "3",
                  fillColor = ~factpal(SU$lrmm_SU),
                  popup=paste0("Susc=", round(SU$lrmm_SU, digits=3)
                  )
                  
                  )})
}
shinyApp(ui, server)

Thanks beforehand!

CodePudding user response:

The problem si your use of the input in your data filtering. Your sliderInput is a range, so two values, but you filter it using it as it was a single value. Either you do not make it a range in the UI, or you filter it correctly in your server part. If it's a range, using a reactive value to create/modify your data on the fly is your best bet.

Also, your popup and your fill coloring were both looking into the complete SU dataset, not the filtered one. You need to use again your new reactive to get the right value:

library(shiny)    # for shiny apps
library(RColorBrewer)
library(leaflet)  # renderLeaflet function
#library(spData)   # loads the world dataset 
SU        = sf::st_read(dsn = "SU.shp") 
SU$lrmm_SU= round(SU$lrmm_SU, digits=3)
factpal <- colorFactor(brewer.pal(n = 4, name ="BuPu") , SU$lrmm_SU) 



ui = fluidPage(
  sliderInput(inputId = "life", "Mixed effects predictions for SU",
              min(SU$lrmm_SU, na.rm = TRUE), 
              max(SU$lrmm_SU, na.rm = TRUE), 
              value = range(SU$lrmm_SU, na.rm = TRUE),
              step=0.1),
  leafletOutput(outputId = "map", width = "100%")
)
server = function(input, output) {

  data <- reactive({
    dat <- SU[SU$lrmm_SU > min(input$life), ]
    dat <- dat[dat$lrmm_SU < max(input$life), ]
    return(dat)
  })
  
  output$map <-  renderLeaflet({
    leaflet() %>% 
      addProviderTiles("Esri.WorldGrayCanvas", group = "Gray") %>%
      addPolygons(data = data(),
                  stroke = TRUE, 
                  fillOpacity = 0.5, 
                  weight = 0.2,
                  opacity = 1,
                  color = "gray",
                  dashArray = "3",
                  fillColor = ~factpal(data()$lrmm_SU),
                  popup=paste0("Susc=", round(data()$lrmm_SU, digits=3)
                  )
                  
      )})
}
shinyApp(ui, server)

To find these types of mistakes, I suggest you print your input in an observe(). It always helps figuring these little bugs!

  • Related