extracting values from leaflet::addDrawToolbar function

2,554 views
Skip to first unread message

Matt S.

unread,
May 11, 2016, 3:02:30 PM5/11/16
to Shiny - Web Framework for R

Hello,

I have installed the development version of leaflet so I can use some of the leaflet.draw plugin functionalities. My end goal is to allow a user of my Shiny app to draw a custom polygon on the leaflet map. From which, I can use the polygon coordinates to subset spatial data.

My specific question concerns the addDrawToolbar() function: How do I extract the coordinates of user created objects from the returned value of addDrawToolbar()?

I am using the following code in a shiny implementation and --as suggested in the function documentation-- am trying to extract the coordinates using input$drawnItems_create[[3]]$coordinates. Clearly, I'm not following the proper usage because I only get a NULL value returned. Here's my code: 

devtools::install_github("byzheng/leaflet") 
library(shiny)
library(leaflet)

shinyUI(bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 80, right = 10, width = 200,
                actionButton("drawPoints", "Draw polygon")
  )
))

shinyServer(function(input, output, session) {

  output$map <- renderLeaflet({
    addTiles(leaflet())
  })

  # User draws polygon
  observeEvent(input$drawPoints, {
    leafletProxy("map") %>% 
      addDrawToolbar()
    print(input$drawnItems_create[[3]]$coordinates) # this returns NULL
  })
})

Any suggestions or assistance would be appreciated. Thank you,

Matt Schumwinger

Florian Kobierksa Baffie

unread,
Jun 16, 2016, 9:37:04 AM6/16/16
to Shiny - Web Framework for R
Hi Matt,

I'm having exactly the same problem as you with code copied from https://github.com/rstudio/leaflet/issues/227
In the example code provided by "ssoulier", he is using it this way:
> lng = input$mymap_drawnItems_created$geometry$coordinates[[1]]
> lat = input$mymap_drawnItems_created$geometry$coordinates[[2]]
but it doesn't work either...

Have you found a solution in the meantime?

Thanks,         Florian Kobierska

Robin Cura

unread,
Jun 16, 2016, 9:51:45 AM6/16/16
to Florian Kobierksa Baffie, Shiny - Web Framework for R, ecom.sch...@gmail.com
Hi,

I did it with a custom version of leaflet (integrating plugin-draw and heatmap), using `feature` signal instead of `created`.
Here's the code I use to retrieve the user selection (only on rectangle draw, but it's quite easy to adapt) :
https://github.com/RCura/TimeLineEDB/blob/master/server.R#L93-112

(I'm only using the first and only feature that the user did draw).

HTH,

Robin

--
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/cc55a113-7b74-4dec-836f-7c0bd203f749%40googlegroups.com.

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

Robin Cura

unread,
Jun 16, 2016, 9:56:55 AM6/16/16
to Florian Kobierksa Baffie, ecom.sch...@gmail.com, Shiny - Web Framework for R
Forgot : here's my leaflet fork/edit : https://github.com/RCura/leaflet

Try it with  : devtools::install_github("RCura/leaflet")

Robin

Shawn Shi

unread,
Aug 11, 2016, 4:43:44 PM8/11/16
to Shiny - Web Framework for R, fba...@gmail.com, ecom.sch...@gmail.com, robin...@parisgeo.cnrs.fr
Hi Robin,

I am using your version of leaflet for a while and  I find the single layer option is really cool. Recently I find a small issue about your package that the radius of the circle drawn via DrawToolbar won't be updated after editing  (e.g, make the circle larger or smaller) while the coordinate of the circle's center will be updated if I move it. 

So could you give me any comments on that?  Thank you.

Best,
Shawn

Robin Cura

unread,
Aug 19, 2016, 8:27:29 AM8/19/16
to Shawn Shi, Shiny - Web Framework for R, Florian Kobierksa Baffie, Matt S
Hi,

Thanks for your bug report. Tried to understand the problem for some hours, and now that I found, I can't think of a way to fix it :
Drawn features are exported to R in GeoJSON format. GeoJSON doesn't handle the circle geometry, and circles are then stored as points with a radius attribute (outside the geometry).
When the coordinates change, the point change and is thus updated in R.
But when the radius changes, it's only an attribute of the json, not on the geojson, and thus, it"s not updated.

Right now, I can't think of any way to fix this behaviour, appart making some special rules for circles.
I will work on this in a few weeks, can't find the time before.

Sorry for this unresolved (at the time) issue :(

Regards,

Robin
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/c911b713-c864-426a-b4b9-a2fdd4813441%40googlegroups.com.

Jamie M. Kass

unread,
Jul 17, 2017, 10:26:11 PM7/17/17
to Shiny - Web Framework for R, se7en.s...@gmail.com, fba...@gmail.com, ecom.sch...@gmail.com, robin...@parisgeo.cnrs.fr
For those still interested in extracting coordinates from shapes drawn with the draw toolbar in R leaflet (albeit with the new leaflet.extras package), please see this Issues thread:

https://github.com/bhaskarvk/leaflet.extras/issues/77

Jamie M. Kass
PhD Candidate
City College of NY
Reply all
Reply to author
Forward
0 new messages