UseR10085
UseR10085

Reputation: 8198

Why does writeRaster in terra give a warning message?

I am trying to 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)

names(s) <- c("a", "b", "c")

writeRaster(s, paste0(names(s), ".tif"), overwrite=TRUE)

It works with the following warning message

Warning message:
In tools::file_ext(filename) %in% c("nc", "cdf") || isTRUE(list(...)$filetype ==  :
  'length(x) = 3 > 1' in coercion to 'logical(1)'

What does this message means?

Upvotes: 0

Views: 224

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47481

That is a bug that you can ignore. It was fixed in the development version that you can install with install.packages('terra', repos='https://rspatial.r-universe.dev') but it has not made it to CRAN yet.

Upvotes: 0

tlhenvironment
tlhenvironment

Reputation: 349

Your line

paste0(names(s), ".tif")

Actually produces a vector of 3 strings:

paste0(c("a", "b", "c"), ".tif")
[1] "a.tif" "b.tif" "c.tif"

You probably want only one file, with a name such as

paste0(paste(c("a", "b", "c"), collapse = "_"), ".tif")
[1] "a_b_c.tif"

To avoid nested paste calls, I suggest looking into the glue package

Upvotes: 0

Related Questions