Search code examples
rrgdal

SHP file reading getting error in R


Hi I am trying to read and plot on a custom shape file in R which is not a map. This is the code I use and the error I get in return:

library(rgdal)
mySHP<- choose.files()
myFile<- readOGR(mySHP)

Error in ogrListLayers(dsn = dsn) : Cannot open data source


Solution

  • If your file is a shapefile, you need to specify the dsn which is the directory where is saved the shapefile and layer which is the name of the shapefile without the extension. You cannot really do it with choose.files. At least not that simply.

    myFile <- readOGR(dsn='path.to.folder', layer='nameOfShapefile')