convertPaths
is simply a wrapper around gsub
for changing the
first part of a path.
convertRasterPaths
is useful for changing the path to a file-backed
raster (e.g., after copying the file to a new location).
convertPaths(x, patterns, replacements) convertRasterPaths(x, patterns, replacements)
x | For |
---|---|
patterns | Character vector containing a pattern to match (see |
replacements | Character vector of the same length of |
filenames <- c("/home/user1/Documents/file.txt", "/Users/user1/Documents/file.txt") oldPaths <- dirname(filenames) newPaths <- c("/home/user2/Desktop", "/Users/user2/Desktop") convertPaths(filenames, oldPaths, newPaths)#> [1] "C:/home/user2/Desktop/file.txt" "C:/Users/user2/Desktop/file.txt"r1 <- raster::raster(system.file("external/test.grd", package = "raster")) r2 <- raster::raster(system.file("external/rlogo.grd", package = "raster")) rasters <- list(r1, r2) oldPaths <- system.file("external", package = "raster") newPaths <- file.path("~/rasters") rasters <- convertRasterPaths(rasters, oldPaths, newPaths) lapply(rasters, raster::filename)#> [[1]] #> [1] "C:/Eliot/rasters/test.grd" #> #> [[2]] #> [1] "C:/Eliot/rasters/rlogo.grd" #>