Not working - Reactive Slider using leafletProxy()

999 views
Skip to first unread message

julia

unread,
Jan 3, 2017, 3:32:47 PM1/3/17
to Shiny - Web Framework for R

I am a beginner in R mapping and I am trying to build an Shiny app to visualise student satisfaction for all the universities in the UK (values range from 72-97). 

Through leaflet I have mapped the university locations with markers and added popups to see the student satisfaction score.

Now I would like to create a reactive slider which allows to choose a satisfaction value resulting in only the universities that scored at least the score selected are displayed on the map using leafletProxy().

Below you can see my code so far with a screenshot of how it looks. As of now, the slider and the map are displayed and the app runs, but it is not reactive. 

Any tips on how to continue?


ui <- bootstrapPage(



  tags$style
(type = "text/css", "html, body {width:100%;height:100%}"),

  leafletOutput
("uniSmap", width = "100%", height = "100%"),

  absolutePanel
(top = 50, right = 50,

                sliderInput
("range", "Satisfaction Score", min(my_shiny_Data$`Satisfaction (%) 2016 Registered`), max(my_shiny_Data$`Satisfaction (%) 2016 Registered`),

                            value
= range(my_shiny_Data$`Satisfaction (%) 2016 Registered`), step = 1 )
                               
 
)

)




server
<- function(input, output, session) {

 

 
# Reactive expression for the data subsetted to what the user selected

  reactiveData
<- reactive({

    my_shiny_Data
[my_shiny_Data$`Satisfaction (%) 2016 Registered` >= input$range[1] & my_shiny_Data$`Satisfaction (%) 2016 Registered` <= input$range[2],]

 
})


  output$uniSmap
<- renderLeaflet({

   

   
# Use leaflet() here for static map

    uniSmap
= leaflet() %>%

       addTiles
() %>%

      setView
(lng = -2.2, lat = 54.5, zoom = 6) #%>%

 
})

 

 
# Incremental changes to the map performed in an observer.

 

  observe
({

   

    leafletProxy
("uniSmap", data = reactiveData()) %>%

      clearShapes
() %>% clearPopups() %>% clearMarkers() %>%

     

      addMarkers
(lng = my_shiny_Data$Longitude,

                  lat
= my_shiny_Data$Latitude,

                  popup
= paste(my_shiny_Data$Institution, "<br>",

                               
"Overall Satisfaction:", my_shiny_Data$`Satisfaction (%) 2016 Registered`, "<br>"))        

                 

 
})
 

}




shinyApp
(ui = ui, server = server)



Stephen McDaniel

unread,
Jan 3, 2017, 6:24:04 PM1/3/17
to julia, Shiny - Web Framework for R
Hi Julia,

Here is a working solution with  notes on a few topics that are easy to overlook. Leaflet is a bit more complex than other outputs! :)

https://github.com/Stephen-McDaniel/shiny-map-leaflet-filtering

Inline image 1

Best regards,
Stephen McDaniel

Chief Data Scientist
PowerTrip Analytics
Automatic data, analytic & visualization solutions in the cloud.



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/810f6df5-a584-4446-9f21-68f65eb8de06%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Best,
Stephen McDaniel
Principal and Co-Founder, Freakalytics™ LLC
Rapid Analytics to Explore, Understand, Communicate & Act™
www.Freakalytics.com

julia

unread,
Jan 4, 2017, 8:51:26 AM1/4/17
to Shiny - Web Framework for R, julianv...@gmail.com, Stephen....@gmail.com
Hi Stephen

Thank you so much for your time and kind help! That was exactly what was missing!



To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages