1

I am currently working on some raster models. My code has generated about 4k models (all .tif files), but about 10% of these cannot be opened using load("insert path"). I have tried readr(), readRDS() and source(), none of which worked. I have also reran some models but it didn't fix the problem. Why can't I load my .tif files?

load("comparison/mc45bi50/ni1130region33.tif")

Error in load("comparison/mc45bi50/ni1130region33.tif") : bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘ni1130region33.tif’ has magic number 'II*' Use of save versions prior to 2 is deprecated

source("comparison/mc45bi50/ni1130region33.tif")

Error in source("comparison/mc45bi50/ni1130region33.tif") :
comparison/mc45bi50/ni1130region33.tif:2:0: unexpected end of input 1: II*

MLavoie
  • 9,671
  • 41
  • 36
  • 56
tungphung
  • 11
  • 1
  • 2

1 Answers1

3

You should use the raster library. It has the raster() function, which should read your .tif file.

Example, creating a raster ourselves, and saving it on disk, Then try to read:

library(raster)
#> Loading required package: sp

## create raster
r <-  raster(ncols=3, nrows=3)
r[] <- 1:9

# save to disk
tmp <- paste(tempdir(), "myraster.tif", sep = "/")
writeRaster(r, tmp)

## read now
r <- raster(tmp)
Matifou
  • 7,968
  • 3
  • 47
  • 52