I have a RasterStack in R called "preds2" that is 4.1 GB and was outputted from 4 RasterStacks and 2 RasterLayers (wveg, wfps_lag, wfps, ndvi, swt, lu):
cl <- makeCluster(4)
registerDoSNOW(cl)
preds<-foreach(j = 1:nlayers(ndvi))%dopar%{
library(raster)
library(SpaDES)
time <- stack(wveg,wfps_lag[[j]],wfps[[j]],ndvi[[j]],swt[[j]],lu)
names(time) <- c('wveg','wfps_lag','wfps','ndvi','swt','lu')
exp(raster::predict(time,m,const=fact_table,exclude=c("s(wlch)","s(wetid)")))
}
stopCluster(cl)
preds2<-stack(preds)
> preds2
class : RasterStack
dimensions : 6617, 11771, 77888707, 27 (nrow, ncol, ncell, nlayers)
resolution : 0.0002694946, 0.0002694946 (x, y)
extent : -95.69591, -92.52369, 41.71803, 43.50128 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10, layer.11, layer.12, layer.13, layer.14, layer.15, ...
min values : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
max values : 0.001754114, 0.001754114, 0.001754114, 0.001754114, 0.001730909, 0.001601078, 0.081421784, 3.510447853, 5.134697329, 7.547881571, 10.945457688, 13.332227330, 14.864517447, 16.708383138, 16.631466329, ...
I am trying to write this RasterStack to a .tif file but get an error:
> writeRaster(stack(preds3), filename = "C:\\Users\\RL\\Documents\\preds.tif", options="INTERLEAVE=BAND", overwrite=TRUE)
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
cannot open file 'C:\Users\RL\AppData\Local\Temp\RtmpeegnBN\raster\r_tmp_2020-04-25_004109_2364_37231.gri': No such file or directory
Same error for using calc
on the object "preds2" as well. I've created much smaller RasterStacks before with this code without any problem. Online blogs and documentation suggest this error may be due to it being such a large RasterStack (e.g. suggestion on storing "preds2" as a rasterTmpFile
but I still get the same error when reading the temp file). Suggestions with code (as I'm new to R) would be appreciated. Thanks!
Figured it out -- the problem was that the RasterStack was too large to be stored in R's working memory and was insteaad saved to a temporary file on my computer that, for whatever reason, I couldn't find or access in R. So I just specified the temporary working directory.
options(rasterTmpDir = "C:\\Users\\RL\\Temp_R_folder")