saveRSTBX: Save and Read RStoolbox Classification Results

View source: R/saveRSTBX.R

saveRSTBXR Documentation

Save and Read RStoolbox Classification Results


Saves objects of classes unsuperClass, superClass, rasterPCA and fCover to file. Useful to archive the fitted models.


saveRSTBX(x, filename, format = "raster", ...)




RStoolbox object of classes c("fCover", "rasterPCA", "superClass", "unsuperClass")


Character. Path and filename. Any file extension will be ignored.


Character. Driver to use for the raster file


further arguments passed to writeRaster


The output of writeRSTBX will be at least two files written to disk: a) an .rds file containing the object itself and b) the raster file (depending on the driver you choose this can be more than two files).


  • saveRSTBX: Save RStoolbox object to file

  • readRSTBX: Read files saved with saveRSTBX


All files must be kept in the same directory to read the full object back into R by means of readRSTBX. You can move them to another location but you'll have to move *all* of them (just like you would with Shapefiles). In case the raster file(s) is missing, readRSTBX will still return the object but the raster will be missing.

writeRSTBX and readRSTBX are convenience wrappers around saveRDS, readRDS. This means you can read all files created this way also with base functionality as long as you don't move your files. This is because x$map is a Raster* object and hence contains only a static link to the file on disk.


## Not run: 
input <- brick(system.file("external/rlogo.grd", package="raster"))
## Create filename
file  <- paste0(tempdir(), "/test", runif(1))
## Run PCA
rpc   <- rasterPCA(input, nSample = 100)
## Save object
saveRSTBX(rpc, filename=file)
## Which files were written?
list.files(tempdir(), pattern = basename(file))
## Re-read files
re_rpc <- readRSTBX(file)
## Remove files 
file.remove(list.files(tempdir(), pattern = basename(file), full = TRUE))

## End(Not run)

RStoolbox documentation built on March 18, 2022, 5:37 p.m.