I'm creating a few points to plot on a Leaflet-R map. However, when trying to plot the points with addCircles I am getting the error:
Warning: Error in derivePoints: Point data not found; please provide addCircles with data and/or lng/lat arguments
The info about the spatial data frame reads:
Simple feature collection with 3 features and 4 fields
geometry type: POINT
dimension: XY
bbox: xmin: -79.48837 ymin: 43.66537 xmax: -79.29187 ymax: 43.71345
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
I have another spatial data frame that works and all the info is the same except that +datum=WGS84 is replaced with +ellps=WGS84. Could that be it? If so, how do I make this change?
Sample Code
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap")
)
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 (
id = c(1, 2, 3),
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() %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
# Centre the map in the middle of Toronto
setView(lng = -79.384293,
lat = 43.685, #43.653908,
zoom = 11) %>%
addCircles(tree_data)
})
}
shinyApp(ui, server)
You need to specify data = tree_data
, because within the addCircles()
call the data
argument is the last one.
leaflet() %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircles(data = tree_data)
The first argument in the addCircles()
call is the map
, which is passed in through the %>%
operator. The second argument is lng
, so what you're doing in your call is equivalent to
addCircles(lng = tree_data)
Another way to send the data into the addCircles()
function is through the leaflet()
call
leaflet(tree_data) %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircles()