# NOT RUN {
# check if R can run the distance functions
aux <- c(
length(suppressWarnings(packageDescription("raster"))),
length(suppressWarnings(packageDescription("gdistance"))),
length(suppressWarnings(packageDescription("sp"))),
length(suppressWarnings(packageDescription("tools"))),
length(suppressWarnings(packageDescription("rgdal"))))
missing.packages <- sapply(aux, function(x) x == 1)
if (any(missing.packages)) {
message("Sorry, this function requires packages '",
paste(c("raster", "gdistance", "sp", "tools", "rgdal")[missing.packages], collapse = "', '"),
"' to operate. Please install ", ifelse(sum(missing.packages) > 1, "them", "it"),
" before proceeding.")
} else {
if (suppressWarnings(require("rgdal"))) {
# move to a temporary directory
old.wd <- getwd()
setwd(tempdir())
# Fetch location of actel's example files
aux <- system.file(package = "actel")[1]
# create a temporary spatial.csv file
file.copy(paste0(aux, "/example_spatial.csv"), "spatial.csv")
# import the shape file and use the spatial.csv
# to check the extents.
x <- loadShape(path = aux, shape = "example_shapefile.shp",
coord.x = "x", coord.y = "y", size = 20)
# Build the transition layer
t.layer <- transitionLayer(x)
# compile the distances matrix. Columns x and y in the spatial dataframe
# contain the coordinates of the stations and release sites.
distancesMatrix(t.layer, coord.x = 'x', coord.y = 'y')
# return to original directory
setwd(old.wd)
rm(old.wd)
} else {
message("Sorry, it appears that rgdal is not being able to load.")
}
}
rm(aux, missing.packages)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab