[Rd] str() on raster objects fails for certain dimensions

From: Henrik Bengtsson <hb_at_biostat.ucsf.edu>
Date: Mon, 31 Jan 2011 11:16:59 -0800


Hi,

str() on raster objects fails for certain dimensions. For example:

> str(as.raster(0, nrow=1, ncol=100))

 'raster' chr [1, 1:100] "#000000" "#000000" "#000000" "#000000" ...

> str(as.raster(0, nrow=1, ncol=101))

Error in `[.raster`(object, seq_len(max.len)) : subscript out of bounds

This seems to do with how str() and "[.raster"() is coded; when subsetting as a vector, which str() relies on, "[.raster"() still returns a matrix-like object, e.g.

> img <- as.raster(1:25, max=25, nrow=5, ncol=5);
> img[1:2]

     [,1] [,2] [,3] [,4] [,5] [1,] "#0A0A0A" "#3D3D3D" "#707070" "#A3A3A3" "#D6D6D6" [2,] "#141414" "#474747" "#7A7A7A" "#ADADAD" "#E0E0E0" compare with:

> as.matrix(img)[1:2]

[1] "#0A0A0A" "#3D3D3D"

The easy but incomplete fix is to do:

str.raster <- function(object, ...) {
  str(as.matrix(object), ...);
}

Other suggestions?

> sessionInfo()

R version 2.13.0 Under development (unstable) (2011-01-27 r54129) Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:

[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):

/Henrik



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 31 Jan 2011 - 19:20:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Feb 2011 - 14:40:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive