[R-sig-Geo] Error in reading shape file with readOGR()

Adam Fera adamfera25 at gmail.com
Tue Feb 13 20:19:57 CET 2018


Hello, Amos,

I came across this posting because I had a similar problem using R for
mapping within GNU Emacs Org Mode on GNU/Linux.  I work on a research team
that uses a shared directory for data analysis.  This makes it imperative
to use relative pathnames so that everyone can use the exact same code, the
exact same way, and achieve the exact same result.  Unfortunately, the
readOGR function in the rgdal package does not allow the use of relative
pathnames in the dsn (data source name) argument.

To get around this, I use the setwd and getwd functions.  I use setwd to
set the working directory to the directory with the shapefile.  Then, I use
the getwd function as the dsn argument.  Since setwd() can handle relative
pathnames and getwd() prints the absolute pathname, this allows the user a
way around the dsn= argument requiring an absolute pathname.  This worked
for my research team and allowed us all to use the same code for mapping in
R.

My R code looks something like this:

## SET WORKING DIRECTORY TO BE UNIFORM
setwd("~/relative/path/to/file/nypp_15c/")

## IMPORT THE SHAPEFILE TO A SPATIAL DATA.FRAME
library(rgdal)
precincts.2015 <- readOGR(
    dsn=getwd(),
    layer="nypp")

I would like to thank all the R developers, in particular Professor Bivand,
for their extremely useful contributions in creating software for
geospatial data analysis.

I hope this helps,

Adam G. Fera

-- 
Adam G. Fera, MPhil
Doctoral Student
Instructor, NYPD Executive MA Police Leadership Program
Research Analyst, Misdemeanor Justice Project
PhD Program in Criminal Justice
John Jay College of Criminal Justice
524 West 59th Street
New York NY 10019
adamfera25 at gmail.com

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list