How do I pass a layerId to a function in a leaflet popup?

658 views
Skip to first unread message

JP

unread,
Jun 22, 2015, 10:38:55 AM6/22/15
to shiny-...@googlegroups.com
I am making a shiny app that has a map. On the map are some circles, and in the addCircles command there is a layerId.  Lets call the data that makes the circles CircleData() - its reactive, and the Id  is CircleName.  I want to use the popup= argument to add a popup that uses the CircleName of that particular circle to run a function, which then returns some text for the popup. Different circles will get different text. depending on the ID. I have tried



1)
addCircles(data=CircleData(),layerId=CircleData$Name, popup=paste( MyFuction(x=CircleData()$CircleName) )   )


This creates the circles correctly, but passes the entire CircleData()$CircleName column to MyFuction, rather than just the value for one particular circle.



2) 

addCircles(data=CircleData(), layerId=CircleData$Name, popup=paste( MyFuction(x=~CircleName) )   )




This crashes as it does not read ~CircleName as a character input, but rather an input of type 'language'

3)
 
addCircles(data=CircleData(), layerId=CircleData$Name, popup=paste( MyFuction(x=input$MyMap_shape_click$id) )   )

Close, I think. If I use this code in a panel  elsewhere in the app, I get the correct output. In a popup, however, the popup briefly flashes the correct output, and then disappears. I think I am establishing a reactive link between the circles and input$MyMap_shape_click$id that is causing the circles to be redrawn every time I click the map.

4)

addCircles(data=CircleData(), layerId=CircleData$Name, popup=isoalte(paste( MyFuction(x=input$MyMap_shape_click$id) ) )  )




putting an isolate around the whole popup does not work. It seems that  the function does not get run at all.Same thing happens if I do an isolate just around MyFunction.

So - any ideas how to pass the CircleName, but not redraw the circles?

Thanks




JP

unread,
Jun 22, 2015, 11:01:25 AM6/22/15
to shiny-...@googlegroups.com
Just noticed in my post above I had "layerId=CircleData$Name" instead of "CircleData()$CircleName" . In my code I have it correctly, it is just a typo in this post, sorry.

Joe Cheng

unread,
Jun 22, 2015, 1:16:25 PM6/22/15
to JP, shiny-...@googlegroups.com
Note that the popup argument takes a character vector, where each element corresponds to one layer being created (with recycling if necessary). So for all but the shortest and most trivial popups, I think this'll be wasteful.

Instead, consider a separate addPopups call in response to click:

observeEvent(input$mapid_shape_click, {
  e <- input$mapid_shape_click
  addPopups(e$lng, e$lat, MyFunction(x=e$id))
})

On Mon, Jun 22, 2015 at 8:01 AM JP <john...@operamail.com> wrote:
Just noticed in my post above I had "layerId=CircleData$Name" instead of "CircleData()$CircleName" . In my code I have it correctly, it is just a typo in this post, sorry.

--
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/8aaf3450-ae6f-44a7-a2c9-47f954a70605%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Cheng

unread,
Jun 22, 2015, 1:17:22 PM6/22/15
to JP, shiny-...@googlegroups.com
Oh whoops, let me try again. Replace mapid with your leafletOutput's outputId:

observeEvent(input$mapid_shape_click, {
  e <- input$mapid_shape_click
  leafletProxy("mapid") %>% addPopups(e$lng, e$lat, MyFunction(x=e$id))
})

JP

unread,
Jun 22, 2015, 1:43:51 PM6/22/15
to shiny-...@googlegroups.com, john...@operamail.com
Joe,
 That did it. Thanks once again for all of your help.

John Paul
Reply all
Reply to author
Forward
0 new messages