3

I want to read a shapefile(polyline) into R, using rgdal and raster packages.

The code looks like:

d<-file("/Volumes/Environment_layers/original/WaterRoad/Road.shp")
o<-shapefile(d)

but I got a warning:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘shapefile’ for signature ‘"file"’

I am new in using R dealing with spatial layers.

3
  • Does this related post help?: gis.stackexchange.com/q/19064
    – Aaron
    Commented Mar 1, 2019 at 7:46
  • @Aaron NOT a duplicate. User here has mistakenly called file on the path and passed a file object (hence the error). I suspect this might be a pythonic paradigm from someone used to python. Either way, its a specific error that other people might encounter and so is worth having as a question as my explanation details the solution.
    – Spacedman
    Commented Mar 1, 2019 at 7:49
  • @Spacedman Reopened, thanks for the heads-up.
    – Aaron
    Commented Mar 1, 2019 at 7:51

1 Answer 1

4

You are doing this:

d <- file("/Volumes/Environment_layers/original/WaterRoad/Road.shp")

which is calling file on the path. This opens the file for reading by R's general reading and writing routines. The shapefile function handles all this so you don't pass it a file object, you pass it the path as a string.

This should work:

d <- "/Volumes/Environment_layers/original/WaterRoad/Road.shp"
o <- shapefile(d)
1
  • Thank you so much!!! yes it works well for me. Also I tried another way, using:
    – Lili Li
    Commented Mar 1, 2019 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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