I want to write rasters by layer using terra package. I am using the following code
library(terra)
# first create a raster
r1 <- r2 <- r3 <- rast(nrow=10, ncol=10)
# Assign random cell values
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- c(r1, r2, r3)
s
plot(s)
writeRaster(s, names(s), overwrite=TRUE)
It gives me following error
Error: [writeRaster] cannot open file: C:/Users/nn/Desktop/lyr.1 In addition: Warning message: C:/Users/nn/Desktop/lyr.1: No such file or directory (GDAL error 4)
I want to have the same output available in raster package using the following function
raster::writeRaster(s, names(s), bylayer=TRUE, format='GTiff', overwrite=TRUE)