I'm trying to use saveRDS()
to save a large number of lists each containing a raster layer and a list with metadata. It worked fine when the raster layer was extracted from a ncdf file, but when the original file is an ascii file, saveRDS()
only writes a pointer to the original file instead of writing the values to the end file.
Here's a condensed version of what's going on:
require(raster)
mf <- raster('myfile.asc')
meta <- list(mylonglistofmetadata)
res <- list(mf, meta)
saveRDS(res, 'myresult.Rdata')
myresult.Rdata
is now simply a 33KB pointer to myfile.asc
, when I really would like it to store the values so it will still work after I erase myfile.asc
(so it should be about 15MB)
In contrast, for other files in ncdf format:
require(ncdf4)
require(raster)
ff <- 'myfile2.nc'
nc <- nc_open(ff)
meta <- list(mylonglistofmetadata)
res <- list(nc, meta)
saveRDS(res, 'myresult2.Rdata')
Here, myresult2.Rdata
is storing everything just like I want it to, so my guess is that the issue arises with the raster package?
Anyone has any idea on how to fix this? I would prefer not to use writeRaster()
, since I'm trying to keep the metadata together with the data, and use the same format as in my batch extracted from ncdf files to ease later processing.
The short answer is that you can do:
Now, the values are in memory and will be saved to the .RData file
Also note that:
You can save metadata with the data via writeRaster (see ?raster::metadata
you can access ncdf files (with geographic data) via
raster('myfile2.nc')
your example for the ncdf file is not informative, as you do not actually use
nc
for anything. If you replacedmf
withnc
it would not work either after you removed'myfile2.nc'