4

Based on the comments below I've explicitly broken out lat/long in the spatial data frame.

Added

addCircleMarkers( ~ longitude, ~ latitude)

Added

observeEvent(input$map_marker_click, { 
    p <- input$map_marker_click
    print(p)
  })

Yet, nothing shows up in the console when I click the markers so I'm still confused.

Revised Code

# Click on circle and get info

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap"),
  fluidRow(verbatimTextOutput("click_text"))
)

server <- function(input, output, session) {

  # Create tree geometries
  tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
  tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
  tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))

  # Create sfc object with multiple sfg objects
  points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)

  # Create tree attributes
  data <- data.frame (
    layerId = c("001", "002", "003"),
    address = c(10, 20, 30),
    street = c("first", "second", "third"),
    tname = c("oak", "elm", "birch"),
    latitude = c(43.6760766531298, 43.6653747165064, 43.7134458013647),
    longitude = c(-79.2918671415814, -79.4883669334101, -79.2964680812039)  
)

  tree_data <- st_sf(data, geometry = points_sfc)

  output$mymap <- renderLeaflet({
    leaflet(data = tree_data) %>%
      addProviderTiles(providers$Stamen.Watercolor) %>%

      # Centre the map in the middle of Toronto
      setView(lng = -79.384293, 
              lat = 43.685, 
              zoom = 11) %>% 

      addCircleMarkers( ~ longitude, ~ latitude)
  })

  observeEvent(input$map_marker_click, { 
    p <- input$map_marker_click
    print(p)
  })


}

shinyApp(ui, server)

When a user clicks on each marker I would like some relevant info displayed below the map. Based on this earlier post I tried this. However, nothing happens when I click on the marker. It may have something to do with my not understanding how to associate markers with layerIds?

# Click on circle and get info

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap"),
  fluidRow(verbatimTextOutput("click_text"))
)

server <- function(input, output, session) {

  # Create tree geometries
  tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
  tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
  tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))

  # Create sfc object with multiple sfg objects
  points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)

  # Create tree attributes
  data <- data.frame (
    layerId = c("001", "002", "003"),
    address = c(10, 20, 30),
    street = c("first", "second", "third"),
    tname = c("oak", "elm", "birch")
)

  tree_data <- st_sf(data, geometry = points_sfc)

  output$mymap <- renderLeaflet({
    leaflet(data = tree_data) %>%
      addProviderTiles(providers$Stamen.Watercolor) %>%

      # Centre the map in the middle of Toronto
      setView(lng = -79.384293, 
              lat = 43.685, 
              zoom = 11) %>% 

      addCircleMarkers()
  })

  observe({
    click <- input$map_marker_click
    if(is.null(click))
      return()

    address <- paste("Address: ", click$street)
    output$click_text <- renderText({
      address
    })

  })

}

shinyApp(ui, server)
ixodid
  • 2,180
  • 1
  • 19
  • 46
  • possibly a duplicate: https://stackoverflow.com/a/42798668/5977215 – SymbolixAU Jul 16 '18 at 01:14
  • `addCircleMarkers` has arguments `popup` and `popupOptions` for having popup labels when the user clicks on it. You'll probably find it much easier to use those rather than fiddling about with `observe` – Conor Neilson Jul 16 '18 at 03:08
  • I require the info to be used in a sidebar not in a pop-up. I've looked at the link mentioned by SymbolixAU and revised my question but am still unable to get info from the click event. – ixodid Jul 16 '18 at 03:48
  • Is there a way to get the info from the popup when you click the circle marker? – AyeTown Aug 06 '19 at 00:22

1 Answers1

4

When you 'observe' something on the map, you need to reference the map you're observing. You do this using this structure

output$<map_id>_event_to_observe

So, in your example your map_id is mymap, hence you'll need to use

observeEvent(input$mymap_marker_click, { 
    p <- input$mymap_marker_click
    print(p)
  })
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • 1
    Thanks. Now the p <- input$mymap_marker_click returns contains $lat, $long and $.nonce. How do I relate these values to the attributes in my data frame. For example, how do I determine the street of the particular circle that was selected? – ixodid Jul 16 '18 at 22:08
  • @ixodid In the answer I linked to in my comment under your question it says "The `id` value relates to the `layerId` you specify in the shape plotting function". You need to specify the `layerId` value in the plotting function. This will be returned to you as the `id` in the click event. You then use this to relate to your data. – SymbolixAU Jul 16 '18 at 22:10
  • OK. I get it now. Thank you for your patience. – ixodid Jul 17 '18 at 00:02
  • Is there a way to get the info from the popup when you click the circle marker? – AyeTown Aug 06 '19 at 00:25
  • @ACE It will depend on how you fill the popup. When you observe the click, you can get an 'id' value back which will point to an 'id' in your data. If your popup information is a column of data you can get it from here. For a more detailed answer you should probably ask a new question. – SymbolixAU Aug 06 '19 at 03:14