I typically use Matlab and am only an amateur when it comes to R. I have been trying to use someone elses code from 2011 and it is calling for a function called 'xyValues'. I have found that this used to be function within the Raster package (version 1.5.8) but no longer appears in the current version. The old raster package is incompatible with newer versions of R. I have tried downgrading R but then that causes other issues.
I have downloaded the tar.gz file and extracted the code for the xyValues function. I tried to paste it into my newer version of the Raster package, but unsurprisingly, it didn't work. Is there an easy way for me to turn the code for this function into an actual function that I can save and then use (as if I wrote the function myself)? I could do this easily in matlab, but it seems the process for writing a function is a little different in R and I don't really know where to start? I see some variations of this question on this site, but I'm wondering if it is possible for a new user of R (like me) to be able to do what I am suggesting. Perhaps it is only possible if you have a lot of knowledge regarding R. This is the code I extracted:
# Author: Robert J. Hijmans
# contact: r.hijmans@gmail.com
# Date : November 2008
# Version 0.9
# Licence GPL v3
if (!isGeneric("xyValues")) {
setGeneric("xyValues", function(object, xy, ...)
standardGeneric("xyValues"))
}
setMethod("xyValues", signature(object='Raster', xy='SpatialPoints'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE,...) {
callGeneric(object, coordinates(xy), method, buffer, fun, na.rm, ...)
}
)
setMethod("xyValues", signature(object='Raster', xy='data.frame'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE,...) {
callGeneric(object, as.matrix(xy), method, buffer, fun, na.rm, ...)
}
)
setMethod("xyValues", signature(object='Raster', xy='vector'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE, ...) {
if (length(xy) == 2) {
callGeneric(object, matrix(xy, ncol=2), method, buffer, fun, na.rm, ...)
} else {
stop('xy coordinates should be a two-column matrix or data.frame, or a vector of two numbers.')
}
} )
setMethod("xyValues", signature(object='RasterLayer', xy='matrix'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE, ...) {
if (dim(xy)[2] != 2) {
stop('xy has wrong dimensions; it should have 2 columns' )
}
if (! is.null(buffer)) {
if (method != 'simple') { warning('method argument is ignored when a buffer is used') }
return( .xyvBuf(object, xy, buffer, fun, na.rm=na.rm) )
}
if (method=='bilinear') {
return(.bilinearValue(object, xy))
} else if (method=='simple') {
cells <- cellFromXY(object, xy)
return(.readCells(object, cells))
} else {
stop('invalid method argument. Should be simple or bilinear.')
}
}
)
setMethod("xyValues", signature(object='RasterStack', xy='matrix'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE, ...) {
.xyvStackBrick(object, xy, method, buffer, fun, na.rm, ...)
} )
setMethod("xyValues", signature(object='RasterBrick', xy='matrix'),
function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE, ...) {
.xyvStackBrick(object, xy, method, buffer, fun, na.rm, ...)
} )
.xyvStackBrick <- function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE, ...) {
dots <- list(...)
layer <- dots$layer
n <- dots$nl
nls <- nlayers(object)
if (is.null(layer)) { layer <- 1 }
if (is.null(n)) { n <- nls }
layer <- min(max(1, round(layer)), nls)
n <- min(max(1, round(n)), nls-layer+1)
if (dim(xy)[2] != 2) {
stop('xy has wrong dimensions; there should be 2 columns only' )
}
if (! is.null(buffer)) {
if (method != 'simple') { warning('method argument is ignored when a buffer is used') }
return( .xyvBuf(object, xy, buffer, fun, na.rm, layer=layer, n=n) )
}
if (method == 'bilinear') {
result <- .bilinearValue(object, xy, layer=layer, n=n)
return(result)
} else if (method=='simple') {
cells <- cellFromXY(object, xy)
return( cellValues(object, cells, layer=layer, n=n) )
} else {
stop('invalid method argument. Should be simple or bilinear.')
}
}
As a followup to this question, initially, when I wanted to see if this function was in my raster package I tried typing help(xyValues)
and nothing came up (because it didn't exist). However, when I try this for functions that DO exist within the package, they too, are not showing up. Does this mean my raster package isn't loaded correctly?
The piece of code I am using with the function in it is:
elevgrid <- xyValues(elev,cbind(xygrid[,2],xygrid[,1]))
where elev
is a Formal class Rasterlayer of size 920x1000 and xygrid
is 4800 obs of 2 variables (x y coordinates)
I tried using:
> source("C:/Users/Documents/raster/R/xyValues.R")
but I got all these errors:
in method for ‘xyValues’ with signature ‘object="Raster",xy="data.frame"’: no definition for class “Raster”
in method for ‘xyValues’ with signature ‘object="Raster",xy="vector"’: no definition for class “Raster”
in method for ‘xyValues’ with signature ‘object="RasterLayer",xy="matrix"’: no definition for class “RasterLayer”
in method for ‘xyValues’ with signature ‘object="RasterStack",xy="matrix"’: no definition for class “RasterStack”
in method for ‘xyValues’ with signature ‘object="RasterBrick",xy="matrix"’: no definition for class “RasterBrick”
Warning message:
in method for ‘xyValues’ with signature ‘object="Raster",xy="SpatialPoints"’: no definition for classes “Raster”, “SpatialPoints”
I tried the getvalues
and approxNA
functions and got this:
> elevgrid <- getValues(elev,cbind(xygrid[,2],xygrid[,1]))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘getValues’ for signature ‘"RasterLayer", "matrix", "missing"’
> elevgrid <- approxNA(elev)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘approxNA’ for signature ‘"RasterLayer"’