[R] terminate R program when trying to access out-of-bounds array element?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Apr 14 03:24:30 CEST 2005


On 4/13/05, Vivek Rao <rvivekrao at yahoo.com> wrote:
> I want R to stop running a script (after printing an
> error message) when an array subscript larger than the
> length of the array is used, for example
> 
> x = c(1)
> print(x[2])
> 
> rather than printing NA, since trying to access such
> an element may indicate an error in my program. Is
> there a way to get this behavior in R? Explicit
> testing with the is.na() function everywhere does not
> seem like a good solution. Thanks.

If you can restrict yourself to arrays of dimension > 1 e.g.

> x <- matrix(1)
> print(x[1,2])
Error in print(x[1, 2]) : subscript out of bounds

then R already does that.




More information about the R-help mailing list