R Language spatial analysis Importing a shape file (.shp)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

rgdal

ESRI shape files can easily be imported into R by using the function readOGR() from the rgdal package.

library(rgdal)
shp <- readORG(dsn = "/path/to/your/file", layer = "filename")

It is important to know, that the dsn must not end with / and the layer does not allow the file ending (e.g. .shp)

raster

Another possible way of importing shapefiles is via the raster library and the shapefile function:

library(raster)
shp <- shapefile("path/to/your/file.shp")

Note how the path definition is different from the rgdal import statement.

tmap

tmap package provides a nice wrapper for the rgdal::readORG function.

library(tmap)    
sph <- read_shape("path/to/your/file.shp")


Got any R Language Question?