Error in readOGR() function: Cannot open data source

I',m trying to create an object with name "trainData' and where 'samples_area ' is a shapefile layer with polygons only. I've set the directory to the folder that contains 'samples_area'

code line:
trainData <- readOGR("samples_area", "polygons")

Error message:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source

Someone, please rectify my mistake.

This error means that rgdal was unable to open the file. It is most often caused by misspelled file name - are you certain your shapefile has no extension?

Solution:

trainData <- readOGR("C:/Users/Zainab Khan/Desktop/india", "samples_area")

here, india is shapefile containing folder, samples_area is shapefile name.
no .shp extension needed. THis worked for me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

With my little knowledge in R (m new in R programming), I read somewhere that if you are using readOGR() function do not add file extension. Yes the ESRI shapefile has .shp extension...

After reading you, I added .shp (I thinks that's what you want to say), but the problem persists.

Cpde:
trainData <- readOGR("C:/Users/Zainab Khan/Desktop/india/train.shp", "polygons")

Error Message:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source