
Set the variable name of a SpatRaster within a pipe
Source:R/spat_set_raster_varnames.R
set_raster_varnames.RdA small wrapper function that assigns a single variable name varnames to a
SpatRaster and returns the object, making it suitable for use in a
magrittr pipe without breaking the chain.
names vs varnames
terra stores two distinct name slots:
names(per-layer) – used in plots,as.data.frame(), and subsetting. Set vianames(x) <- ...orterra::set.names().varnames(per-data-source) – a single string inherited from the source file variable name (e.g. NetCDF). For any in-memory or derivedSpatRasterthere is always exactly one data source regardless of the number of layers, sovarnamesonly ever holds one value.
Examples
library(terra)
r <- terra::rast(
ncols = 10, nrows = 10, nlyr = 3, vals = 1L, names = c("l1", "l2", "l3"))
# setting variable names directly with terra
terra::varnames(r) <- "v1"
r
#> class : SpatRaster
#> size : 10, 10, 3 (nrow, ncol, nlyr)
#> resolution : 36, 18 (x, y)
#> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
#> source : spat_20fe8f55571_8446_BnO3HOrWuPceBcV.tif
#> varname : v1
#> names : l1, l2, l3
#> min values : 1, 1, 1
#> max values : 1, 1, 1
# Set variable name within a pipe
(r2 <- set_raster_varnames(r, "temperature"))
#> class : SpatRaster
#> size : 10, 10, 3 (nrow, ncol, nlyr)
#> resolution : 36, 18 (x, y)
#> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
#> source : spat_20fe8f55571_8446_BnO3HOrWuPceBcV.tif
#> varname : temperature
#> names : l1, l2, l3
#> min values : 1, 1, 1
#> max values : 1, 1, 1
# Reset variable name to empty
(r3 <- set_raster_varnames(r, NULL))
#> class : SpatRaster
#> size : 10, 10, 3 (nrow, ncol, nlyr)
#> resolution : 36, 18 (x, y)
#> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
#> source : spat_20fe8f55571_8446_BnO3HOrWuPceBcV.tif
#> names : l1, l2, l3
#> min values : 1, 1, 1
#> max values : 1, 1, 1