title: Identify position of a click on a raster in leaflet, in R
body: <p>I am plotting a large lat-lon NetCDF <code>raster</code> over an R <code>leaflet</code> map using <code>shinydashboard</code>. When I click on the map, a popup comes out and shows row, column, lat-lon position and value of the clicked raster point. (See reproducible code below)</p>
  
  <p>The problem is that I am experiencing a shift in the raster if the raster is large enough. For example, here I clicked on a point which should have a value, but the result is that the identified point is the one above.</p>
  
  <p><a href="https://i.stack.imgur.com/eP4kL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/eP4kL.png" alt="enter image description here"></a></p>
  
  <p>I believe this has to do with the fact that the raster used by <code>leaflet</code> is projected, while the raw data I use to identify the points is Lat-Lon, since the clicked point is returned as Lat-Lon by <code>leaflet</code>. I cannot use the projected file (<code>depth</code>) since its units are in meters, not degrees!
  Even if I tried to reproject those meters to degrees, I got a shift.</p>
  
  <p>Here is a basic runnable example of the code:</p>
  
  <pre><code>#Libraries
  library(leaflet)
  library(raster)
  library(shinydashboard)
  library(shiny)
  
  #Input data
  download.file("https://www.dropbox.com/s/y9ekjod2pt09rvv/test.nc?dl=0", destfile="test.nc")
  inputFile    = "test.nc"
  inputVarName = "Depth"
  lldepth &lt;- raster(inputFile, varname=inputVarName)
  lldepth[Which(lldepth&lt;=0, cells=T)] &lt;- NA #Set all cells &lt;=0 to NA
  ext &lt;- extent(lldepth)
  resol &lt;- res(lldepth)
  projection(lldepth) &lt;- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
  
  #Project for leaflet
  depth &lt;- projectRasterForLeaflet(lldepth)
  
  #Prepare UI
  sbwidth=200
  sidebar &lt;- dashboardSidebar(width=sbwidth)
  body &lt;- dashboardBody(
            box( #https://stackoverflow.com/questions/31278938/how-can-i-make-my-shiny-leafletoutput-have-height-100-while-inside-a-navbarpa
              div(class="outer",width = NULL, solidHeader = TRUE, tags$style(type = "text/css", paste0(".outer {position: fixed; top: 50px; left: ", sbwidth, "px; right: 0; bottom: 0px; overflow: hidden; padding: 0}")),
              leafletOutput("map", width = "100%", height = "100%")
              )
            )
          )
  ui &lt;- dashboardPage(
    dashboardHeader(title = "A title"),
    sidebar,
    body
  )
  #
  #Server instance
  server &lt;- function(input, output, session) {
    output$map &lt;- renderLeaflet({#Set extent
      leaflet()  %&gt;%
        fitBounds(ext[1], ext[3], ext[2], ext[4])
    })
  
    observe({#Observer to show Popups on click
      click &lt;- input$map_click
      if (!is.null(click)) {
        showpos(x=click$lng, y=click$lat)
      }
    })
  
    showpos &lt;- function(x=NULL, y=NULL) {#Show popup on clicks
      #Translate Lat-Lon to cell number using the unprojected raster
      #This is because the projected raster is not in degrees, we cannot use it!
      cell &lt;- cellFromXY(lldepth, c(x, y))
      if (!is.na(cell)) {#If the click is inside the raster...
        xy &lt;- xyFromCell(lldepth, cell) #Get the center of the cell
        x &lt;- xy[1]
        y &lt;- xy[2]
        #Get row and column, to print later
        rc &lt;- rowColFromCell(lldepth, cell)
        #Get value of the given cell
        val = depth[cell]
        content &lt;- paste0("X=",rc[2],
                          "; Y=",rc[1],
                          "; Lon=", round(x, 5),
                          "; Lat=", round(y, 5),
                          "; Depth=", round(val, 1), " m")
        proxy &lt;- leafletProxy("map")
        #add Popup
        proxy %&gt;% clearPopups() %&gt;% addPopups(x, y, popup = content)
        #add rectangles for testing
        proxy %&gt;% clearShapes() %&gt;% addRectangles(x-resol[1]/2, y-resol[2]/2, x+resol[1]/2, y+resol[2]/2)
      }
    }
  
    #Plot the raster
    leafletProxy("map") %&gt;%
      addRasterImage(depth, opacity=0.8, project=FALSE, group="Example", layerId="Example", colors=colorNumeric(terrain.colors(10), values(depth), na.color = "black"))
  }
  
  
  print(shinyApp(ui, server))
  </code></pre>
  
  <p>How can I correctly identify the points, if the raster is large?</p>
  
  <p>EDIT:
  I also wanted to provide some additional links to (possibly) related documentation or questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/33742107/raster-image-seems-to-be-shifted-using-leaflet-for-r?rq=1">Raster image seems to be shifted using leaflet for R</a></li>
  <li><a href="https://stackoverflow.com/questions/34583442/r-for-leaflet-redirect-when-clicking-on-raster-image">R for leaflet redirect when clicking on raster image</a></li>
  <li><a href="https://gis.stackexchange.com/questions/183918/is-it-possible-to-use-a-rasterclick-event-within-an-interactive-leaflet-map">https://gis.stackexchange.com/questions/183918/is-it-possible-to-use-a-rasterclick-event-within-an-interactive-leaflet-map</a></li>
  <li><a href="https://stackoverflow.com/questions/28938642/marker-mouse-click-event-in-r-leaflet-for-shiny">marker mouse click event in R leaflet for shiny</a></li>
  </ul>