Display polygons on leaflet map within R Shiny

423 views
Skip to first unread message

cho7tom

unread,
Mar 18, 2015, 8:49:45 AM3/18/15
to shiny-...@googlegroups.com
dear all,

I have a code to display specific polygons on a map :

library(rgdal)
library(leaflet)

download.file(file.path('http://www.naturalearthdata.com/http/',
                        'www.naturalearthdata.com/download/50m/cultural',
                        'ne_50m_admin_0_countries.zip'), 
              f <- tempfile())
unzip(f, exdir=tempdir())

world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')

commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh",  "Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus", "Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "Kiribati", "Lesotho", "Malawi", "Malaysia", "Maldives", "Malta", "Mauritius", "Mozambique", "Namibia", "Nauru", "New Zealand", "Nigeria", "Pakistan", "Papua New Guinea", "Rwanda", "St. Kitts and Nevis", "Saint Lucia", "St. Vin. and  Gren.", "Samoa", "Seychelles", "Sierra Leone", "Singapore", "Solomon Is.",  "South Africa", "Sri Lanka", "Swaziland", "Tanzania", "Tonga", "Trinidad and  Tobago", "Tuvalu", "Uganda", "United Kingdom", "Vanuatu", "Zamibia")

leaflet() %>% addTiles() %>% addPolygons(data=subset(world, name %in% commonwealth), weight=2)

I cannot succeed in displaying this output in a shiny app, such as Joe Cheng"s example: http://glimmer.rstudio.com/jcheng/leaflet-demo/. the bold piece of code is what I don't know how to fine-tune..

I am not familiar with JavaScript (yet!), could you please show me how to proceed?

So far I have in my ui.R:
leafletMap("map", width="100%", height="100%", 
                                           initialTileLayer = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                                           options = list(center = c(50.93, 4.12), zoom = 3)
                                           )

and in my server.R:
map <- createLeafletMap(session, "map")

Many thanks in advance for your help !

Thomas

William Beasley

unread,
Mar 19, 2015, 12:52:43 AM3/19/15
to shiny-...@googlegroups.com
I know this really isn't answering your question, but I suggest starting with one of Joe's newer mapping examples (unless there's something specific about that population map that you need to replicated).  The SuperZip example is the most similar to the one you linked.  http://shiny.rstudio.com/gallery/

Joe Cheng

unread,
Mar 19, 2015, 4:59:33 PM3/19/15
to William Beasley, shiny-...@googlegroups.com
Actually Thomas, your code is using the new leaflet API while my SuperZip example is using the old one. I think you just want to do this:

Add this to your ui.R:
leafletOutput("map")

Add this to your server.R:
output$map <- renderLeaflet({
  leaflet() %>% addTiles() %>% addPolygons(...whatever...)
})

On Wed, Mar 18, 2015 at 9:52 PM, William Beasley <wibe...@hotmail.com> wrote:
I know this really isn't answering your question, but I suggest starting with one of Joe's newer mapping examples (unless there's something specific about that population map that you need to replicated).  The SuperZip example is the most similar to the one you linked.  http://shiny.rstudio.com/gallery/

--
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-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/bdfa0995-12cc-4b7b-91f6-63df065ea3c5%40googlegroups.com.

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

thomas filaire

unread,
Mar 19, 2015, 5:08:22 PM3/19/15
to Joe Cheng, William Beasley, shiny-...@googlegroups.com
Indeed Joe, I finally found this! Thank you very much!
Looking forward to new rstudio features, they are great!
Regards,
Thomas


--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/QzWftCT2tik/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CAF_jjKqAPhYOYOd48zRYmseH%3DP4m6xoTFHUG34vUvDxeY8g3UQ%40mail.gmail.com.

cho7tom

unread,
Mar 20, 2015, 2:34:13 PM3/20/15
to shiny-...@googlegroups.com, wibe...@hotmail.com
Dear Joe,

I now would like to design some "advanced" popups, and I doubt my approach is the correct one as I am mixing R and HTML code is a way which is quite weird (at least to me :-))
I created a post for that here. I would be more than happy to get your advices and best practices for complete such a task.

I deployed an example over shinyapp.io here as well

Many thanks in advance and best regards,

Thomas
Reply all
Reply to author
Forward
0 new messages