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

The code looks like:


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.

  • Does this related post help?: gis.stackexchange.com/q/19064 – Aaron Mar 1 '19 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 Mar 1 '19 at 7:49
  • @Spacedman Reopened, thanks for the heads-up. – Aaron Mar 1 '19 at 7:51

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)
  • Thank you so much!!! yes it works well for me. Also I tried another way, using: – Lili Li Mar 1 '19 at 8:10

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.