I want to make an animation map in R. I have tried several command lines but I get some errors, as follows:

> library(raster)
> s <- shapefile("//Users//Merry//Desktop//ro_judete_poligon//ro_judete_poligon")
Error in .local(x, ...) : file.exists(extension(x, ".shp")) is not TRUE

or this command

> ro_judete_poligon<-readOGR(dsn="C://Users//Merry//Desktop",layer="ro_judete_poligon.shp")
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open data source

How can I get the shapefile in R? I am confused.

  • In Windows, you use single forward slashes or double backslashes - so these two should be paths to your desktop: C:\\Users\\Merry\\Desktop or C:/Users/Merry/Desktop – Spacedman Mar 27 at 7:39

readOGR syntax can be a bit unfriendly at times. Here's a suggestion.

Assuming your shapefile path is - C:/Users/Merry/Desktop/ro_judete_polgion.shp, the following should work:


#try without specifying a layer
ro_judete_poligon <- readOGR("C:/Users/Merry/Desktop/ro_judete_poligon.shp")
  • I tried your suggestion. And I receive this error: Error in ogrListLayers(dsn = dsn) : Cannot open data source – Ciobotaru Ana-Maria Mar 26 at 17:10
  • I tried again and it works. The right formulation was without [.shp]. Finally, it works! Thanks a lot for the help! – Ciobotaru Ana-Maria Mar 26 at 17:14
  • readOGR either has one argument which is the path to the shapefile with .shp, or two arguments, the first (dsn) is the path of the folder containing the shapefile, and the second (layer) is the shapefile name without .shp. – Spacedman Mar 27 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.