How to solve this error when I read shp file.

I follow this tutorial and try to read this shp file.

I try several ways in R but fail.
I also read this answer from SO but it doesn't help me.

chi.poly <- maptools::readShapePoly('foreclosures.shp')
#> Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
#> Error in getinfo.shape(filen): Error opening SHP file

Created on 2018-12-12 by the reprex package (v0.2.1)

So I try readOGR and st_readError functions.

file.exists('data/foreclosures.shp')
#> [1] TRUE
chi.poly <- rgdal::readOGR('data/foreclosures.shp')
#> Error in ogrListLayers(dsn = dsn): Cannot open data source

Created on 2018-12-12 by the reprex package (v0.2.1)

file.exists('data/foreclosures.shp')
#> [1] TRUE
chi.poly <- sf::st_readError('data/foreclosures.shp')
#> Error: 'st_readError'不是'namespace:sf'内的出口对象:

Created on 2018-12-12 by the reprex package (v0.2.1)

Anyone know how to do?

If you have your shapefile stored inside a folder named data, directly under your working directory, you can easily read it with this line of code :

library(rgdal)
chi.poly <- readOGR("./data", layer = "foreclosures")    
1 Like

Did you download the other 2 files? A shapefile requires the .shp as well as the .shx and .dbf files.

1 Like

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