Saturday, 20 April 2019

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


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.



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...