0
votes

I am new to Shiny and R in general so I'm sure this is a very simple problem but I have spent a verrry long time trying to figure it out and have gotten nowhere so any help would be greatly appreciated! What I have right now is based largely off of this post Leaflet R shiny: select & zoom

I am trying to create an app that will let the user define which country they want to view and then the app will zoom to that country. Each country is defined by the lat, lng of the centroid, a zoom level and the name. The code I have is as such:

ui

var.zoom <- setNames(as.numeric(country_centroid$COUNTRY),country_centroid$COUNTRY)

shinyUI(navbarPage("SnailViz", theme = shinytheme("united"),
tabPanel("Map",
fluidPage(
fluidRow(column(width =3,id = "visualization", fixed = TRUE,
             h1("Visualization", align = "center"),
wellPanel(fluidRow(selectInput("countryZoom", label = h3("Zoom to"), 
                                  choices = var.zoom)))),
column( 9, id = "mapp", fixed =TRUE,
        leafletOutput("map", width = "100%", height= 850))
))))

server.R

shinyServer(function(input, output, session) {

output$map <- renderLeaflet({
leaflet() %>%
addPolygons( data = climateZones, fillOpacity = 0.5, fillColor = ~pal_grid(climateZones$Climate), stroke = FALSE) %>%
addLegend(pal = pal_grid, values = climateZones$Climate, title = "Climate Zones") %>%
addProviderTiles("CartoDB.Positron",group="CartoDB.Positron",options = providerTileOptions(opacity = 1))  %>%
setView(lat=1.701620, lng = 17.27356, zoom = 4)
})

Zooming <-reactive({
subset(country_centroid, COUNTRY == input$countryZoom)})

observe({
leafletProxy("map") %>%
setView(lng=Zooming()$X,lat=Zooming()$Y,zoom = Zooming()$Level)
})
})

country_centroid

 X       Y    Level   COUNTRY
 17.27    1.70  4   Africa
 -1.74   12.28  8   Burkina Faso
 29.88   -3.37  8   Burundi
 12.74    5.69  8   Cameroon
 23.65   -2.87  8   Congo DRC
 29.51    26.3  8   Egypt
 39.63    8.62  8   Ethiopia
 -1.20    7.97  8   Ghana
 -5.55    7.63  8   Ivory Coast
 37.81    0.60  8   Kenya
 34.30  -13.21  8   Malawi
 -3.52   17.36  8   Mali
 -8.89   29.11  8   Morocco
  8.11    9.62  8   Nigeria
-14.45   14.36  8   Senegal
-11.78    8.56  8   Sierra Leone
 30.01   16.05  8   Sudan
 34.80   -6.26  8   Tanzania
  9.57   34.11  8   Tunisia
 32.38    1.27  8   Uganda
 27.79  -13.45  8   Zambia
 29.87  -19.00  8   Zimbabwe

Basically everything appears as I would expect it to, but when I change the input using the dropdown, the map does not respond. My understanding of this is very basic, but I assume I have some problem with the observe(), but I have tried many things from other posts and am basically at a loss at this point. Thank you in advance!!

1
Welcome to SO. A good question always includes the data so that it is reprod.. In you case please provide us with country_centroidTonio Liebrand
Thanks! I have added it in.rhimz
Missing climateZonesHubertL

1 Answers

0
votes

Using var.zoom as choices of the selectInput, input$countryZoom is a numeric, so you have to use as.numeric in subset as well:

subset(country_centroid, as.numeric(COUNTRY) == input$countryZoom))