Stack Exchange
Best way to draw on leaflet map in R
I want to draw several things on a leaflet map (through Shiny/R)
I initialize the map like this
map = leaflet() %>% addProviderTiles("Stamen.TonerLite") %>% setView(-1.5, 53.4, 9)
output$myMap = renderLeaflet(map)

Then, depending on what is clicked in the App I ether want to draw Markers or a Polygon
sp <- reactiveValues()
ep <- reactiveValues()
area <- reactiveValues()
area$mp <- matrix(...) # empty matrix with 2 cols named lat/lng

observeEvent(input$map_click, {
    coords <- input$map_click
    if ( (!is.null(as.integer(input$button)) && (!is.null(coords))) ) {
        if (as.integer(input$button) == 1) {
            sp[["lat"]] <- coords$lat
            sp[["lng"]] <- coords$lng
        } else if (as.integer(input$button) == 2) {
            ep[["lat"]] <- coords$lat
            ep[["lng"]] <- coords$lng
        } else if (as.integer(input$button) == 3) {
            cm <- matrix(data = c(coords$lat, coords$lng), nrow = 1, ncol = 2)
            area$mp <- rbind(area$mp, cm)
        } else {
            print("Kawum!")
        }
})

What I cannot get into my head is how to draw something now on the leaflet map.
What is group ID, what is layer ID. Where comes leafletProxy into play?
How would I, depending on which if else statement kicks in, send the data to leaflet and add a marker or a polygon? 
Any help or pointing into the right direction is highly appreciated!

Maybe this can clarify things:
library(shiny)
library(leaflet)

ui <- shinyUI(fluidPage(
  actionButton("button", "Change style!"),
  leafletOutput("myMap")
))

server <- function(input, output){

map = leaflet() %>% addProviderTiles("Stamen.TonerLite") %>% setView(-1.5, 53.4, 9)
output$myMap = renderLeaflet(map)

sp <- reactiveValues()
ep <- reactiveValues()
area <- reactiveValues()

observeEvent(sp$lat, {
  leafletProxy("myMap") %>% addMarkers(lat = sp$lat, lng = sp$lng)
})

observeEvent(ep$lat, {
  leafletProxy("myMap") %>% addCircles(lat = ep$lat, lng = ep$lng)
})

observeEvent(area$mp, {
  leafletProxy("myMap") %>% addPolygons(lat = area$mp[ , 1], lng = area$mp[ , 2])
})

observeEvent(input$myMap_click, {
    coords <- input$myMap_click
    if ( (!is.null(input$button) && (!is.null(coords))) ) {
        if (input$button %% 4 == 1) {
            sp[["lat"]] <- coords$lat
            sp[["lng"]] <- coords$lng
        } else if (input$button %% 4 == 2) {
            ep[["lat"]] <- coords$lat
            ep[["lng"]] <- coords$lng
        } else if (input$button %% 4 == 3) {
            cm <- matrix(data = c(coords$lat, coords$lng), nrow = 1, ncol = 2)
            area$mp <- if(!is.null(area$mp)){rbind(area$mp, cm)}else{cm}
        } else {
            print("Kawum!")
        }
    }
})

}

shinyApp(ui, server)

First thing, the click event needs to be named after the output element. So input$myMap_click gives you the coords. Second, the leaflet proxy is designed to draw points, things etc. into existing maps. Imagine you'd always re-render the map to do leaflet() %>% addMarkers(...). leafletProxy just needs the output element's name and draws the markers on top of it. 
The code above shows some things you can do with that. E.g. using the polygons. 
Try using it and comment, if there is something unclear. 

Thanks, but that is not working. Warning: Unhandled error in observer: Invalid map parameter observeEvent(sp$lat). I checked, within the observeEvent(sp$lat, {... the coords for lat and lng: They come out properly... I tried to wrap it into a null check like so ... if (!is.null(sp$lat)) {                 leafletProxy("myMap" %>%  addMarkers(lat... , still same error...

@Stophface I am using R-3.2.5 on Windows with all packages up to date. What do you use?

leaflet_1.0.0, shiny_0.12.2, R version 3.2.3 (2015-12-10), Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.10.5 (Yosemite)

I used update.packages("leaflet"), for shiny too. Still the same error message.

I have versions Leaflet: 1.0.1 and Shiny 0.13.2. Could you update R and see if the problem remains? I don't know what else could be wrong... R version 3.3.0 is out.

Should have restarted R before updating shiny/leaflet. I get a different error message now when I try to set a marker where I click. Stack trace (innermost first):     79: dispatch     78: invokeMethod     77: eval     76: eval     75: %>%     74: addMarkers     73: function_list[[k]]     72: withVisible     71: freduce     70: _fseq     69: eval     68: eval     67: withVisible     66: %>%     65: structure     64: leafletProxy     63: observeEventHandler [/...server.R#45]      1: shiny::runApp

The error seems to be the same. dispatch sends the message 'Invalid map parameter`. Please provide your example. Maybe you could append it to / edit your initial question.

Let us continue this discussion in chat.
