Rasters are sometimes file-based, so the normal save and copy and assign mechanisms in R don't work for saving, copying and assigning. This function creates an explicit file copy of the file that is backing the raster, and changes the pointer (i.e., filename(object)) so that it is pointing to the new file.

  repoDir = NULL,
  overwrite = FALSE,
  drv = getOption("reproducible.drv", RSQLite::SQLite()),
  conn = getOption("reproducible.conn", NULL),



The raster object to save to the repository.


Character denoting an existing directory in which an artifact will be saved.


Logical. Should the raster be saved to disk, overwriting existing file.


an object that inherits from DBIDriver, or an existing DBIConnection object (in order to clone an existing connection).


A DBIConnection object, as returned by dbConnect().


Not used


A raster object and its newly located file backing. Note that if this is a legitimate Cache repository, the new location will be a subdirectory called rasters/ of repoDir/. If this is not a repository, the new location will be within repoDir.


library(raster) # make a cache repository a <- Cache(rnorm, 1)
#> No cacheRepo supplied and getOption('reproducible.cachePath') is inside a temporary directory; #> this will not persist across R sessions.
#> ...(Object to retrieve (7072c305d8c69df0.rds))
#> loaded cached result from previous rnorm call,
r <- raster(extent(0,10,0,10), vals = 1:100) # write to disk manually -- will be in tempdir() r <- writeRaster(r, file = tempfile()) # copy it to the cache repository r <- .prepareFileBackedRaster(r, tempdir()) r # now in "rasters" subfolder of tempdir()
#> class : RasterLayer #> dimensions : 10, 10, 100 (nrow, ncol, ncell) #> resolution : 1, 1 (x, y) #> extent : 0, 10, 0, 10 (xmin, xmax, ymin, ymax) #> crs : NA #> source : C:/Users/emcintir.L-VIC-A023744/AppData/Local/Temp/RtmpamdaBp/rasters/filea04264140ba.grd #> names : layer #> values : 1, 100 (min, max) #>