0

I am getting an error, saying could not find the function "shapefile". I also tried all the possible solutions. I did the shp, .shx and .dbf files in the same folder and many more but it doesn't work, any help, please?

library(raster)
setwd("filename of the folder ")
S <-shapefile(datafolder,"file.shp"))

then error

Error in shapefile(dataFolder, "file.shp")) : could not find function "shapefile"

user20650
  • 24,654
  • 5
  • 56
  • 91
Addisu
  • 1
  • 1

1 Answers1

0

Here I show that/how raster::shapefile works.

library(raster)
filename <- system.file("external/lux.shp", package="raster")
filename
#[1] "C:/soft/R/R-4.1.1/library/raster/external/lux.shp"
p <- shapefile(filename)
p
#class       : SpatialPolygonsDataFrame 
#features    : 12 
#extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#crs         : +proj=longlat +datum=WGS84 +no_defs 
#variables   : 5
#names       : ID_1,     NAME_1, ID_2,   NAME_2, AREA 
#min values  :    1,   Diekirch,    1, Capellen,   76 
#max values  :    3, Luxembourg,   12,    Wiltz,  312 
 

Your

S <-shapefile(datafolder,"file.shp"))

is not proper R syntax (parenthesis do not match). Perhaps you want

S <- shapefile("file.shp")

or

S <- shapefile(file.path(datafolder,"file.shp"))

in which case you do not need to set the working directory

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63