Adding multi polygon for leaflet map in Shiny

3,041 views
Skip to first unread message

Harri Hytönen

unread,
Feb 9, 2014, 1:42:32 PM2/9/14
to shiny-...@googlegroups.com
Hi,

We are trying to add multi polygon layer for our leaflet map. Is there already any solution for using leaflet L.multiPolygon whit R?

We are familiar with addPolygon and able to draw one polygon at the time, but we should be able to handle many polygons at the same time.

-Harri

eunji noh

unread,
Apr 8, 2014, 7:37:53 PM4/8/14
to shiny-...@googlegroups.com
I tried to use addPolygon for multiple times. But all the polygons are shown in really short time and only last one remains in the plot. Anyone having success on this?
or should I use layer?

-Eunji

Harri Hytönen

unread,
Apr 9, 2014, 1:18:26 AM4/9/14
to shiny-...@googlegroups.com
Hi,

We managed to "solve" this problem. Actually solution is already in the function, in binding.js comment you can find out that you should just add null value before every new polygon. We did solve this problem also that way that we made our own function that can read list of polygons and show them on map.

If you have already add null values, you should also check that every polygon has also Id. If you have less Ids than polygons some of your polygons will not be shown.

-Harri

Dawg

unread,
Apr 9, 2014, 11:41:55 AM4/9/14
to shiny-...@googlegroups.com
Can you share some code snippets? Thanks!
D.

Martijn Tennekes

unread,
May 9, 2014, 4:28:25 AM5/9/14
to shiny-...@googlegroups.com
I had the same question, and already found the answer:-)
Null values in javascript are equivalent to NA values in R. This will work:

opts1=list(color='white', fillColor='red')
opts2=list(color='white', fillColor='blue')

map$addPolygon(
                        c(51,52,52,51,51, NA, 53, 54, 54, 53, 53),
                        c(5, 5, 6, 6, 5, NA, 7, 7, 8, 8, 7),
                        layerId=c("1", "2"),
                        options=list(opts1, opts2))

Dawg

unread,
May 10, 2014, 6:42:36 PM5/10/14
to shiny-...@googlegroups.com
Thanks!

Franco Peschiera

unread,
May 12, 2014, 2:02:41 PM5/12/14
to Dawg, shiny-...@googlegroups.com
Thanks Martijn, for the example.

I'm trying to add a popup to the polygon when a user clicks, similar to the the circles popup in the leaflet example. I copied and was going to adapt server code related to the clicks (shown below) but then I realized that there is not lat, long property for polygons (at least the input$map_shape_click did not return values for those two).

I searched in the bindings.js to add something like a lat and a long being the centroid of the coordinates when creating the polygon but discovered I do not understand javascript enough. Is it possible to add this functionality easily? (or maybe it already existed somewhere?)

thanks!

  observe({
    event <- input$map_shape_click
    if (is.null(event))
      return()
    map$clearPopups()

    isolate({
      cities <- topCitiesInBounds()
        city <- cities[row.names(cities) == event$id,]
        selectedCity <<- city
        content <- as.character(tagList(
          tags$strong(paste(city$City, city$State)),
          tags$br(),
          sprintf("Estimated population, %s:", input$year),
          tags$br(),
          prettyNum(city[[popCol()]], big.mark=',')
        ))
      map$showPopup(event$lat, event$lng, content, event$id)
    })
  })

--
Sent from my netBook


Thanks!
--
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.
For more options, visit https://groups.google.com/d/optout.

Franco Peschiera

unread,
May 16, 2014, 3:03:10 PM5/16/14
to Dawg, shiny-...@googlegroups.com
Hi again,

I just realized i do not actually need to get that information after all, I'll just use the set of data I have to complement whatever is missing, as long as I know the ID of the polygon I created and I am able to match it with my original data.

regards,

--
Sent from my netBook


Joe Cheng

unread,
May 17, 2014, 3:37:55 AM5/17/14
to Franco Peschiera, Dawg, shiny-...@googlegroups.com
That's the right approach I think; I use the same approach in my leaflet apps.

nora

unread,
Mar 24, 2015, 11:03:33 AM3/24/15
to shiny-...@googlegroups.com, franco.p...@gmail.com, dag.l...@katrisk.com


Hi, I have a quick question regarding the polygon plotting. I have a dataset with polygon's lat and lon coordinates and a third column 'id', that looks like:

> head(pol, 15)
      lat    lon tid
1  43.195 -3.195   1
2  43.195 -3.190   1
3  43.190 -3.190   1
4  43.190 -3.195   1
5  43.195 -3.195   1
6      NA     NA   1
7  43.215 -3.130   2
8  43.215 -3.125   2
9  43.210 -3.125   2
10 43.210 -3.130   2
11 43.215 -3.130   2
12     NA     NA   2
13 43.305 -3.035   3
14 43.305 -3.030   3
15 43.300 -3.030   3

I did not have any problem to plot the polygons in with Shiny with following code:

opts1=list(color='white', fillColor='red')

map$addPolygon(pol$lat, pol$lon, layerId=unique(pol$tid), defaultOptions=opts1)

Nevertheless, when I try address and plot only one polygon, I do not get anything:

id=45
map$addPolygon(pol[pol$tid==id, ]$lat, pol[pol$tid==id, ]$lon, layerId=unique(pol[pol$tid==id, ]$tid), defaultOptions=opts1)

I would appreciate some help, thanks!

nora

unread,
Mar 24, 2015, 11:12:13 AM3/24/15
to shiny-...@googlegroups.com, franco.p...@gmail.com, dag.l...@katrisk.com
I noticed It is a problem with the layer identifiers (maybe it is also the reason that the event "map_shape_mouseover" over the polygons is not returning any 'id'); but I still do not know who to fix it. I have carried out the plotting of markerCircles passing as well a data frame column of 'ids' as LayerId, and it is working well. 

Joe Cheng

unread,
Mar 24, 2015, 12:08:04 PM3/24/15
to nora, shiny-...@googlegroups.com, Franco Peschiera, Dag Lohmann
Hmmm. Can you try printing `pol[pol$tid==id, ]` and make sure it's not empty?

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

nora

unread,
Mar 24, 2015, 8:09:33 PM3/24/15
to shiny-...@googlegroups.com, bago...@gmail.com, franco.p...@gmail.com, dag.l...@katrisk.com
Thanks Joe, I managed to print it providing the LayerId as a list, although I still do not get why it requires a list when giving one polygon, and not when passing the whole data frame.

On the other hand, I still have problems to pop up over the polygons. I have markers and polygons on the same map. I am using same code (below) for markers and polygons (just changing the event name: map_marker_mouseover/click for map_shape_mouseover/click)

  showPopup2 <- function(id, lat, lng) {
    content <- as.character(tagList(
      tags$h4("Tile Id:")
    ))
    map$showPopup(lat, lng, content, id)
  }
  
  clickObs2 <- observe({
    map$clearPopups()
    event <- input$map_shape_click  
    if (is.null(event))
      return()
    
    isolate({
      showPopup2(event$id, event$lat, event$lng)
    })
  })
  
  session$onSessionEnded(clickObs2$suspend)

Any hint what could be happening?

Thanks for your help

Joe Cheng

unread,
Mar 25, 2015, 12:31:01 AM3/25/15
to nora, shiny-...@googlegroups.com, Franco Peschiera, Dag Lohmann
The showPopup2 function does not appear to actually include the ID in the message, only the words "Tile Id:"...? Did you mean tags$h4("Tile Id:", id) ?

--
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.
Reply all
Reply to author
Forward
0 new messages