[GIS] Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘shapefile’ for signature ‘”NULL”’

csvdbfrwildlife-ecology

I've used the following code to generate home range isopleths using r:

library(rhr)
library(rgeos)

#list all *.txt files.
fls <- list.files(path = "C:/Users/evan/Desktop/mydir", pattern = "*.txt", 
full.names = TRUE)


#read files into a list. 
dat <- lapply(fls, read.table, sep = ",", header = TRUE)
#get file names. 
nms <- tools::file_path_sans_ext(list.files(path = 
"C:/Users/evan/Desktop/mydir", pattern = "*.txt"))
names(dat) <- nms

locoh <- lapply(dat, function(x) rhrLoCoH(x[, c("LONGITUDE", "LATITUDE")]))


dir.create("out")
for (i in seq_along(locoh)) {
  shapefile(rhrIsopleths(locoh[[i]]), file.path("out", paste0(nms[i], ".shp")))
}
#This is where I get the error message.

#calculate home ranges by seasonal range.

for (season in c("Winter", "Summer", "Migration")) {
  #read files into a list
  locoh_s <- lapply(dat, function(x) {
    xx <- x[x$RANGE == season, c("LONGITUDE", "LATITUDE")]
    if (nrow(xx) > 10) { # threshold of at least 10 relocations
      rhrLoCoH(xx[ ])  
    } else {
      NA
    }
  })

  locoh_s <- locoh_s[sapply(locoh_s, is.list)]

  for (i in seq_along(locoh_s)) {
    shapefile(rhrIsopleths(locoh_s[[i]]), file.path("out", 
paste0(names(locoh_s)[i], "_", season, ".shp")))

  }
}
##This is where I get the error message again.

This code has been tested and it works on comma delimited text files which I manually created from opening the attribute table in ArcMap and clicking "Export." However, when I try and use an application (created by "WhiteTown" and downloaded off the internet) to batch generate comma delimited .csv or .txt files from .dbf files.

I get the following error:

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

As far as I can tell, the .txt files are identical. I've spent a good deal of time studying them trying to understand why the code would work on the Arc-generated data, but not the .dbf-to-.csv generated data.

Here's an example of what one of my datasets looks like:

https://drive.google.com/file/d/0BzrdU1u3e23zbDJCelFEd295czA/view?usp=sharing

EDIT: I can make the data work if I introduce a limited number of rows. For example I no longer get an error when the code looks like this:

locoh <- lapply(dat, function(x) rhrLoCoH(x[1:30, c("LONGITUDE", "LATITUDE")])) ## Garbage 1:30 added make the code work.

for (season in c("Winter", "Migration", "Summer")) {
  # read files into a list
  locoh_s <- lapply(dat, function(x) {
    xx <- x[x$RANGE == season, c("LONGITUDE", "LATITUDE")]
    if (nrow(xx) > 10) { 
      rhrLoCoH(xx[1:30, ])  ## Another Garbage 1:30
    } else {
      NA
    }
  })

This seems to be a syntax error about selecting rows and columns that I don't understand.

Or maybe some of the rows are junk and they are screwing up the code. For instance, when I type "1:30" it works, also when I type 1:125, but not if I leave it blank, and not if I type 1:4601.

I'd love if someone could enlighten me.

Best Answer

Lets look closely at the error, and, in conjunction with some knowledge on how R works, figure out what is actually happening, and how to debug it:

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

If you read the help for shapefile:

Usage:

     ## S4 method for signature 'character'
     shapefile(x, stringsAsFactors=FALSE, verbose=FALSE, warnPRJ=TRUE, ...)

     ## S4 method for signature 'Spatial'
     shapefile(x, filename='', overwrite=FALSE, ...)

There are two valid signatures - one for when the first argument is a character, in which case it treats that as a shapefile path and reads it into R, and the second where the first argument is a "Spatial" object and it gets written to a Shapefile specified by the filename argument.

What you've done, somehow, is to call shapefile with a NULL argument. I can replicate your error:

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

It looks like you are trying to write some data to a shapefile, so for some reason which I don't think we can reproduce because we don't have your data, you've got an empty data object, and R is refusing to write a shapefile with no data in it.

Tracing this back in your code, rhrIsopleths(locoh[[i]]) must be a NULL object for some value of i. Which value of i? You need to figure that out and then make sure locoh[[i]] is valid and then figure out why rhrIsopleths(locoh[[i]]) is NULL. Maybe there's no data and hence no isopleths.