5

I am trying to get the country information by clicking on map, but the country name or Long /lat did not get print out.

can anyone enlighten me on this issue?

Thanks

The map is from Natural Earth.

library(rgdal)
library(shiny)
library(leaflet)

folder="."
country <- readOGR(dsn = folder, layer = "ne_110m_admin_0_countries", encoding="UTF-8")
country <- spTransform(country, CRS("+proj=longlat +ellps=GRS80"))

ui<-fluidPage(
  leafletOutput('mymap')
)

server <- function(input, output, session){
  # RV<-reactiveValues(Clicks=list())
  output$mymap<- renderLeaflet(
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = country, layerId= ~ADMIN, fillColor = "red", fillOpacity = 1,  color = "black", 
                  stroke = T,  weight = 1  
      )
  )
  observeEvent(input$map_shape_click, {
    p<- input$map_shape_click 
    print(p$id) # click on map, no response
  }) 
}

shinyApp(ui=ui, server=server)
r2evans
  • 141,215
  • 6
  • 77
  • 149
Heatshock
  • 61
  • 1
  • I've suggested an edit to your question, for formatting: (1) please learn about SO's [formatting](https://stackoverflow.com/help/formatting) and [markdown](https://stackoverflow.com/editing-help), code is much easier to read (for many, at least) when fixed-width and possibly highlighted; (2) having to scroll through pages of code can be a barrier (the "M" in MWE is relevant), so there is a tradeoff between compact and well-styled. In this case, many blank lines were not necessary (IMO) and once removed allows your whole example to fit without scrolling. – r2evans Apr 17 '18 at 15:05
  • I'm not intimately familiar with `leaflet`, but are you certain that `print` is not called? Is it possible that `$id` is not printable? (Just a stab ...) Perhaps https://stackoverflow.com/questions/42507943/click-event-on-leaflet-tile-map-in-shiny#comment72153408_42507943 would be helpful. – r2evans Apr 17 '18 at 15:11

1 Answers1

6

Your map name is "mymap" and not "map". So you need to replace input$map_shape_click with input$mymap_shape_click.

library(rgdal)
library(shiny)
library(leaflet)

folder="."
country <- readOGR(dsn = folder, layer = "ne_110m_admin_0_countries", 
encoding="UTF-8")
country <- spTransform(country, CRS("+proj=longlat +ellps=GRS80"))

ui<-fluidPage(
  leafletOutput('mymap')
)

server <- function(input, output, session){
  # RV<-reactiveValues(Clicks=list())
  output$mymap<- renderLeaflet(
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = country, layerId= ~admin, fillColor = "red", 
fillOpacity = 1,  color = "black", 
                  stroke = T,  weight = 1  
      )
  )
  observeEvent(input$mymap_shape_click, {
    p<- input$mymap_shape_click 
    print(p$id) # click on map, no response
  }) 
}

shinyApp(ui=ui, server=server)
kluu
  • 2,848
  • 3
  • 15
  • 35
  • HI, you are my hero, I thought that map_shape_click is a built function. Thank you very much – Heatshock Apr 18 '18 at 15:19
  • Haha your welcome, happened to me as well, that's how I know now ^^ – kluu Apr 18 '18 at 15:24
  • 1
    Thanks to @kluu. Anything I looked at just was "map_shape_click" in it. So, I thought it is keyword of R like radioButton, ui, server, etc. So, my variable name was different from "variable_shape_click" ... anyhow, thanks Stupid thing wasted my time! – OverFlow Police Jun 26 '19 at 22:57