I'm not sure that this is the solution, but I found a solution that works for my purpose! It appears that the problem was with my click event. When I clicked on a polygon, the result was still only a point rather than a polygon. I subset my ctry
object by the click point and set that object as my data in the final addPolygons()
function. I also added some groups and layerIds.
library(shiny)
library(rgdal)
library(leaflet)
ctry <- readOGR("D:/", layer = "ctry")
proj4string(ctry) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
shinyApp(
ui = fluidPage(leafletOutput("map")),
server = function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = ctry,
fillColor = "gray",
fillOpacity = 1,
weight = 2,
stroke = T,
color = "blue",
opacity = 1,
group = "Countries",
layerId = ~admin)
})
observeEvent(input$map_shape_click, {
click <- input$map_shape_click
if(is.null(click))
return()
#pulls lat and lon from shiny click event
lat <- click$lat
lon <- click$lng
#puts lat and lon for click point into its own data frame
coords <- as.data.frame(cbind(lon, lat))
#converts click point coordinate data frame into SP object, sets CRS
point <- SpatialPoints(coords)
proj4string(point) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
#retrieves country in which the click point resides, set CRS for country
selected <- ctry[point,]
proj4string(selected) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
proxy <- leafletProxy("map")
if(click$id == "Selected"){
proxy %>% removeShape(layerId = "Selected")
} else {
proxy %>% addPolygons(data = selected,
fillColor = "black",
fillOpacity = 1,
color = "red",
weight = 3,
stroke = T,
layerId = "Selected")
}
})
})
Using the RStudio IDE, your code worked fine in the Viewer Pane. However, it didn't worked using Firefox 52.0.1 browser.
Commenting the observer()
function and adding addAwesomeMarkers ()
to the renderLeaflet({})
worked fine in Firefox 52.0.1 browser. I think Firefox 52.0.1 browser was right, because if you are using an observer
function that mean it will draw the markers after the input$map_click
action was done and never before that.
One property of the observer
function is:
"An observer is like a reactive expression in that it can read reactive values and call reactive expressions ..." (from Shiny package documentation)
Try the suggested code below from your reproducible example:
library(shiny)
library(sp)
library(leaflet)
ui <- bootstrapPage (leafletOutput("map", width = "80%", height = "800px"))
server <- function(input, output, session) {
output$map <- renderLeaflet({
print ("render tiles")
b <- bbox (breweries91)
leaflet (breweries91) %>%
# addTiles () %>%
addProviderTiles (providers$CartoDB.Positron) %>%
addAwesomeMarkers () %>%
fitBounds (b[1], b[2], b[3], b[4])
})
# observe ({
# print ("make markers")
# leafletProxy ("map", data = breweries91) %>%
# addAwesomeMarkers ()
# cl <- input$map_click
# if (!is.null (cl))
# print ("click")
# })
}
shinyApp (ui, server)
Also, if you want to preserve the observe
function but not to draw the markers at the begining, you can use it in this way:
observe ({
print ("observe function called")
# leafletProxy ("map", data = breweries91) %>%
# addAwesomeMarkers ()
cl <- input$map_click
if (!is.null (cl))
print ("click")
})
Best Answer
Here's what I did.
First, try assigning it to projection epsg:3857 - this is what Google Maps uses, and is in metres. So I did:
Now I can transform that to lat-long and see where on the earth it thinks it is:
Okay, that's the wrong place. Kentucky is 89W to 82W, a span of 7 degrees, but this was making it about 20 degrees. A factor of three. Which is about the same factor as metres to feet. So I figured it might be one of those American state coordinate systems that uses feet rather than metres.
A little searching led me to EPSG:3091...
http://spatialreference.org/ref/epsg/3091/
So let's reassign and see where that projects to:
Winner winner chicken dinner, as we say over here.
I can plot this on the Kentucky state map from the
maps
package:To use it in
leaflet
you probably need to transform it, so:gives you
add2
in lat-long coordinates.If you want to create a .prj file so that reading the shapefile results in the correct projection, download it from the ".PRJ file" link on the spatial reference web site: http://spatialreference.org/ref/epsg/3091/prj/
It should look something like this: