"Error: could not find valid method" for `"add<-"(sds(x), x)`
Closed this issue · 4 comments
While the S4 method is documented, and found when using showMethods
in my installation, R is unable to find during runtime a signature for the add<-
generic for arguments of type SpatRasterDataset and SpatRaster.
Note that when I load terra I do receive the following warning.
> library('terra')
terra 1.7.78
WARNING: different compile-time and run-time versions of GEOS
Compiled with:3.12.1-CAPI-1.18.1
Running with:3.12.2-CAPI-1.18.2You should reinstall package 'terra'
Here is a reproducible example of the error.
r <- rast(nrows=5, ncols=9, vals=1:45)
x <- c(r, r*2)
add(x) <- r*3
x
sdx <- sds(as.list(x))
add(sdx) <- r*4
> terra::"add<-"
standardGeneric for "add<-" defined from package "terra"
function (x, value)
standardGeneric("add<-")
<bytecode: 0x556d9511b700>
<environment: 0x556d951106d0>
Methods may be defined for arguments: x, value
Use showMethods(add<-) for currently available ones.
> showMethods("add<-")
Function: add<- (package terra)
x="SpatRaster", value="SpatRaster"
x="SpatRasterCollection", value="SpatRaster"
x="SpatRasterDataset", value="SpatRaster"
> showMethods("add<-")
It looks like add
works with SpatRasterDataset, by adding a SpatRaster to the defined subdataset, but not by adding a subdataset.
As the description of the function states:
"Add (in place) a SpatRaster to another SpatRaster or to a SpatRasterDataset or SpatRasterCollection"
Check those two examples:
r <- rast(nrows=5, ncols=9, vals=1:45)
x <- c(r, r*2)
add(x) <- r*3
sdx <- sds(as.list(x))
add(sdx[1]) <- r*4
sdx
> sdx
class : SpatRasterDataset
subdatasets : 3
dimensions : 5, 9 (nrow, ncol)
nlyr : 2, 1, 1
resolution : 40, 36 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source(s) : memory
names : lyr.1, ,
sdx2 <- sds(x)
add(sdx2[1]) <- r*5
sdx2
> sdx2
class : SpatRasterDataset
subdatasets : 1
dimensions : 5, 9 (nrow, ncol)
nlyr : 4
resolution : 40, 36 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source(s) : memory
names : lyr.1
Thanks for your reply, @Rapsodia86, it inspired me to try assigning to a higher index in the SDS to add a new subdataset, which was my intention with the code.
r <- rast(nrows=5, ncols=9, vals=1:45)
x <- c(r, r*2)
add(x) <- r*3
x
sdx <- sds(as.list(x))
# add(sdx) <- r*4
sdx[4] <- r*4 # adds/inserts a new subdataset
Because you state it works as intended, and I've found a way of achieving what I was intending to do, I'm going to close the issue because it doesn't look like anything is broken.