0

I recently started using R projects to organize my files and data.

I have my .Rdata file and scripts all saved within the same folder (which is currently selected as my working directory). In RStudio, I can see all the objects in my Global Environment and I can interact with them in that window but when I try run a line of code that uses one of the objects, I get this error:

> head(current.clim)
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
  cannot open file 'C:\Users\username\AppData\Local\Temp\RtmpkVd7Cc\raster\r_tmp_2020-02-06_100056_20448_29146.gri': No such file or directory

I don't understand why it would be referencing a Temp file (or why these files are saved here at all). Isn't the idea behind an R project to have all this information in one place?

Any help would be much appreciated!

Edit:

> str(current.clim)

Formal class 'RasterStack' [package "raster"] with 11 slots
  ..@ filename: chr ""
  ..@ layers  :List of 2
  .. ..$ :Formal class 'RasterLayer' [package "raster"] with 12 slots
  .. .. .. ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. .. .. .. ..@ name        : chr "C:\\Users\\username\\AppData\\Local\\Temp\\RtmpkVd7Cc\\raster\\r_tmp_2020-02-06_100056_20448_29146.grd"
  .. .. .. .. .. ..@ datanotation: chr "INT2S"
  .. .. .. .. .. ..@ byteorder   : Named chr "little"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "value"
  .. .. .. .. .. ..@ nodatavalue : num -32768
  .. .. .. .. .. ..@ NAchanged   : logi TRUE
  .. .. .. .. .. ..@ nbands      : int 19
  .. .. .. .. .. ..@ bandorder   : Named chr "BIL"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "value"
  .. .. .. .. .. ..@ offset      : int 0
  .. .. .. .. .. ..@ toptobottom : logi TRUE
  .. .. .. .. .. ..@ blockrows   : int 0
  .. .. .. .. .. ..@ blockcols   : int 0
  .. .. .. .. .. ..@ driver      : chr "raster"
  .. .. .. .. .. ..@ open        : logi FALSE
  .. .. .. ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. .. .. .. ..@ values    : logi(0) 
  .. .. .. .. .. ..@ offset    : num 0
  .. .. .. .. .. ..@ gain      : num 1
  .. .. .. .. .. ..@ inmemory  : logi FALSE
  .. .. .. .. .. ..@ fromdisk  : logi TRUE
  .. .. .. .. .. ..@ isfactor  : logi FALSE
  .. .. .. .. .. ..@ attributes: list()
  .. .. .. .. .. ..@ haveminmax: logi TRUE
  .. .. .. .. .. ..@ min       : num -278
  .. .. .. .. .. ..@ max       : num 319
  .. .. .. .. .. ..@ band      : int 1
  .. .. .. .. .. ..@ unit      : chr ""
  .. .. .. .. .. ..@ names     : chr "layer.1"
  .. .. .. ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. .. .. .. ..@ type      : chr(0) 
  .. .. .. .. .. ..@ values    : logi(0) 
  .. .. .. .. .. ..@ color     : logi(0) 
  .. .. .. .. .. ..@ names     : logi(0) 
  .. .. .. .. .. ..@ colortable: logi(0) 
  .. .. .. ..@ title   : chr(0) 
  .. .. .. ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. .. .. .. ..@ xmin: num -180
  .. .. .. .. .. ..@ xmax: num 180
  .. .. .. .. .. ..@ ymin: num -60
  .. .. .. .. .. ..@ ymax: num 90
  .. .. .. ..@ rotated : logi FALSE
  .. .. .. ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. .. .. .. ..@ geotrans: num(0) 
  .. .. .. .. .. ..@ transfun:function ()  
  .. .. .. ..@ ncols   : int 8640
  .. .. .. ..@ nrows   : int 3600
  .. .. .. ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. .. .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
  .. .. .. ..@ history : list()
  .. .. .. ..@ z       : list()
  .. ..$ :Formal class 'RasterLayer' [package "raster"] with 12 slots
  .. .. .. ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. .. .. .. ..@ name        : chr "C:\\Users\\username\\AppData\\Local\\Temp\\RtmpkVd7Cc\\raster\\r_tmp_2020-02-06_100056_20448_29146.grd"
  .. .. .. .. .. ..@ datanotation: chr "INT2S"
  .. .. .. .. .. ..@ byteorder   : Named chr "little"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "value"
  .. .. .. .. .. ..@ nodatavalue : num -32768
  .. .. .. .. .. ..@ NAchanged   : logi TRUE
  .. .. .. .. .. ..@ nbands      : int 19
  .. .. .. .. .. ..@ bandorder   : Named chr "BIL"
  .. .. .. .. .. .. ..- attr(*, "names")= chr "value"
  .. .. .. .. .. ..@ offset      : int 0
  .. .. .. .. .. ..@ toptobottom : logi TRUE
  .. .. .. .. .. ..@ blockrows   : int 0
  .. .. .. .. .. ..@ blockcols   : int 0
  .. .. .. .. .. ..@ driver      : chr "raster"
  .. .. .. .. .. ..@ open        : logi FALSE
  .. .. .. ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. .. .. .. ..@ values    : logi(0) 
  .. .. .. .. .. ..@ offset    : num 0
  .. .. .. .. .. ..@ gain      : num 1
  .. .. .. .. .. ..@ inmemory  : logi FALSE
  .. .. .. .. .. ..@ fromdisk  : logi TRUE
  .. .. .. .. .. ..@ isfactor  : logi FALSE
  .. .. .. .. .. ..@ attributes: list()
  .. .. .. .. .. ..@ haveminmax: logi TRUE
  .. .. .. .. .. ..@ min       : num 0
  .. .. .. .. .. ..@ max       : num 10577
  .. .. .. .. .. ..@ band      : int 12
  .. .. .. .. .. ..@ unit      : chr ""
  .. .. .. .. .. ..@ names     : chr "layer.12"
  .. .. .. ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. .. .. .. ..@ type      : chr(0) 
  .. .. .. .. .. ..@ values    : logi(0) 
  .. .. .. .. .. ..@ color     : logi(0) 
  .. .. .. .. .. ..@ names     : logi(0) 
  .. .. .. .. .. ..@ colortable: logi(0) 
  .. .. .. ..@ title   : chr(0) 
  .. .. .. ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. .. .. .. ..@ xmin: num -180
  .. .. .. .. .. ..@ xmax: num 180
  .. .. .. .. .. ..@ ymin: num -60
  .. .. .. .. .. ..@ ymax: num 90
  .. .. .. ..@ rotated : logi FALSE
  .. .. .. ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. .. .. .. ..@ geotrans: num(0) 
  .. .. .. .. .. ..@ transfun:function ()  
  .. .. .. ..@ ncols   : int 8640
  .. .. .. ..@ nrows   : int 3600
  .. .. .. ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. .. .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
  .. .. .. ..@ history : list()
  .. .. .. ..@ z       : list()
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num -180
  .. .. ..@ xmax: num 180
  .. .. ..@ ymin: num -60
  .. .. ..@ ymax: num 90
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 8640
  ..@ nrows   : int 3600
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
  ..@ history : list()
  ..@ z       : list()
apple
  • 387
  • 1
  • 14
  • Post the output from `str(current.clim)` – IRTFM Feb 06 '20 at 21:44
  • @42- Added the output in the original post – apple Feb 06 '20 at 21:55
  • And I think it answers the question. You can see a reference to temp files in the output. When developers create S4 objects (technically objects with classes that have S4 methods), they may reference external data sources to keep size of objects under control. – IRTFM Feb 06 '20 at 22:00
  • The use of .Rdata files is generally discouraged. Best practice is to used a script that loads necessary packages and builds things from scratch. They yoiu don't get confused about stuff you forgot you had "laying around" in your workspace, or as in this case, stuff you did earlier that you didn't really understand. – IRTFM Feb 06 '20 at 22:05
  • I was hoping this would be an easy solution to avoid having to rerun the base code all the time. Most of my data are quite large and take many hours (sometimes over a day) to execute. – apple Feb 06 '20 at 22:10
  • Weird that it still recognizes the object when I just type "> current.clim" into the console. I get information the dimensions, resolution, projection, etc. of the raster – apple Feb 06 '20 at 22:11
  • I'm not sure why you think it is weird? That object was loaded into memory at the time of startup. – IRTFM Feb 06 '20 at 22:28
  • I have the same problem with a Rdata file. @IRTFM, I don´t know/understand how to do " used a script that loads necessary packages and builds things from scratch". My models takes 4-5 days to execute and the only way to don´t rerun the code is saving the out as .Rdata. Any hint will be more than welcomed. – Juan Carlos Jun 14 '21 at 07:49

0 Answers0