问题
I don't know how to deal with save.image()
and saveRDS()
with raster data in R. I have understood that raster package open a connexion with the image file using raster()
function, so it doesn't really open the file into R workspace.
I want to save my workspace (data.frame, list, raster, etc) with save.image()
function (or similar) and open it in a different computer. If I try to plot or process a raster object saved in a different computer, always have the same issue:
Error in .local(.Object, ...) :
`C:\path\to\file.tif' does not exist in the file system,
and is not recognised as a supported dataset name.
Is there a way to save a raster object (opened as external file) in R format? I don't mean raster format as tiff nor grid and others.
回答1:
You can save rasters, like other R objects, using the save
command.
save(r,file="r.Rdata")
On a different computer, you can load that file using
load("r.Rdata")
which will bring back the raster r
in your workspace.
I have tried this across Windows and Linux and it never gives problems
回答2:
At your own risk, you can use the readAll
function to load the raster into memory before saving. e.g.
r <- raster(system.file("external/test.grd", package="raster"))
r <- readAll(r) # force data into memory
save(r, file = 'r.RData')
It can be loaded on a different machine as mentioned
load('r.Rdata`)
Beware, this will be problematic for very large rasters on memory limited systems
来源:https://stackoverflow.com/questions/40473199/how-to-save-raster-data-in-r-object-format