1

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.

  • If it works on the data from ArcMap then this does not seem to be an ArcMap issue so I have removed the tag for that. I think you need to try and provide a tiny dataset that illustrates where you are stuck. – PolyGeo Jun 8 '17 at 20:37
  • @PolyGeo In the codeblock I've written where I get the error message and I've included a link to my data, and I'm willing to e-mail anyone who thinks they can fix it. I need some serious expertise. – J.W. Powell Jun 8 '17 at 21:10
  • The design of SE does not encourage emailing between its users because the idea is to have all aspects of the question and any answers visible to help other visitors and users who may encounter the same or similar issues in the future. – PolyGeo Jun 8 '17 at 21:36
  • "when I try and use an application to batch generate comma delimited .csv or .txt files from .dbf files" - what is that application? – PolyGeo Jun 8 '17 at 21:37
  • It's called "DBF to CSV converter (64 Bit)." It was created by "WhiteTown" and downloaded off the internet. – J.W. Powell Jun 8 '17 at 21:44
3

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.

  • Thank you very much for this answer @Spacedman. I'm not sure the best way to upload a small dataset to Stack Exchange, but I'd be willing to. Some of the rows of my data are indeed empty, due to the GPS not having enough satellites. I suspect this may be what is causing the problem, because when I run the code on only 30 or so rows it runs just fine. Is there a way to call 'shapefile', but tell it to ignore all NULLs? – J.W. Powell Jun 12 '17 at 14:52
  • @J.W.Powell Instead of editing this answer, post the final solution as a new answer, referring back to this answer which lead you to solve the problem. – Midavalo Jun 12 '17 at 19:00
0

Deleting 0 values for the Latitude and Longitude columns fixed the problem.

I deleted the zero values with the following script.

setwd("C:/Users/Florian/Documents/R/Evan")
data = read.csv("AF_486_2005_culled_geo_Events_shp.csv", sep=",") #read data as matrix (arrays) 

nrow(data) # test how many rows
Not0 <- which(data$LATITUDE == 0) #output which rows = 0
data <- data[-Not0,] # new data = old data with rows != 0
nrow(data) # test how many rows
write.csv(data, file = "AF_486_2005_culled_geo_Events_shp_-00.csv") #output new file

@Spacedman's suggestions helped me diagnose the problem.

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.