readOGR() cannot open file

the Mandatory files should be all in the same directory

.shp — shape format

.shx — shape index format;

.dbf — attribute format;

then we can just give the path as a parameter to the function it will work.

global_24h =readOGR( '/Users/m-store/Desktop/R_Programing/global_24h.shp')


You could have shown that you have the right path with:

list.files('~/R/funwithR/data/ne_110m_land', pattern='\\.shp$')
file.exists('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp')

perhaps try:

readOGR(dsn=path.expand("~/R/funwithR/data/ne_110m_land"), layer="ne_110m_land")

or a simpler alternative that is wrapped around that:

library(raster)
s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")

Update:

rgdal has changed a bit and you do not need to separate the path and layer anymore (at least for some formats). So you can do

x <- readOGR("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")

(perhaps still using path.expand)

Also, if you are still using readOGR you are a bit behind the times. It is better to use terra::vect or sf::st_read.


For me, the command returned the Cannot open layer error when I included the dsn and layer tags.

So when I included it all just as readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp') it worked.

Note that my file was a gjson, so I've only seen this with readOGR('~/R/funwithR/data/ne_110m_land/ne_110m_land.gjson')


I had the same error. To read in a shapefile, you need to have three files in your folder: the .shp, .dbf and .shx files.

Tags:

R

Gdal

Ogr

Rgdal