Search code examples
rleafletgisshapefiler-sf

How to plot SP object as SF, in R leaflet


I just got started with GIS so please don't be mean.

I'm trying to plot different shp files, but this one particular file isn't plotting onto my interactive map I built which uses the leaflet package.

The file itself loads in R fine, but when I insert it into my map it looks super zooms out and looks like this

so I ran this code "attributes(geometry)" and this came up -

$generic
[1] "geometry"
attr(,"package")
[1] "sp"

$package
[1] "sp"

$group
list()

$valueClass
character(0)

$signature
[1] "obj"

$default
`\001NULL\001`

$skeleton
(function (obj) 
stop("invalid call in method dispatch to 'geometry' (no default method)", 
    domain = NA))(obj)

$class
[1] "standardGeneric"
attr(,"package")
[1] "methods"

To me it seems like I'm dealing with "sp" and not "sf".. and something needs to be done about it?

Also this part "stop("invalid call in method dispatch to 'geometry' (no default method)" seems weird too.

Could you please help me fix this?

I'm trying to plot by using the "addPolygons" function in leaflet.

Thanks so much!


Solution

  • It seems you used rgdal::readOGR() to read the shapefile; it produces a Spatial object (in the realm of {sp}, not {sf}).

    I suggest you consider sf::st_read() to get the shapefile into R; it should behave better then.