I've a list of hundreds of rasters that I've renamed, but when I export using writeRaster
, the filenames have not changed.
For example:
library(terra)
library(tidyverse)
# all the of the packages I have loaded during actual work session...
# if (!require("pacman")) install.packages("pacman"); library(pacman)
# p_load(here, tidyverse, sf,lubridate, raster, terra, janitor)
# read list of files
(file_list <- list.files(here("test_set"),
pattern="tif$", full.names = TRUE, recursive=FALSE))
# get names from list
(file_names <- tools::file_path_sans_ext(list.files(here("test_set"),
pattern="tif$", full.names = FALSE)) %>%
# remove everything after 1st underscore
str_extract("[^_]+"))
# read list of rasters
(r <- map(file_list, rast))
# rename the list
(names(r) <- file_names)
map(r, function (i)
writeRaster(i, filename=paste0(here("test_set/new_names//"), names(i),".tif"), overwrite=TRUE))
I'd like to write the rasters to disk using the shortened names.