I am writing a function that expects two arguments: X and Y. Argument X should be a raster which can be generated by packages such as raster
, terra
, or stars
.
Argument Y should be a polygon from packages such as sp
or sf
.
All these packages manage spatial data differently and represent this data using objects of different structure.
How can I ensure that the user supplied objects of the right classes to my function?
Currently, I managed only to test whether object X is of class RasterLayer
(created by the package raster
) and whether the object Y is of class SpatialPolygonsDataFrame
(created by the package sp
)
e.g.
if(class(Y) != "SpatialPolygonsDataFrame"){
stop("Y has to be a spatial object of type SpatialPolygonsDataFrame")
}
if(class(X) != "RasterLayer"){
stop("X has to be an object of class RasterLayer")
}
any
. Something along the lines of:if(!any(x %in% c("SpatRaster", "RasterLayer", "stars") )
you could then issue astop
as the condition.class
.