Vivek Rao
2005-Apr-13 21:26 UTC
[R] terminate R program when trying to access out-of-bounds array element?
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.
Rich FitzJohn
2005-Apr-13 21:51 UTC
[R] terminate R program when trying to access out-of-bounds array element?
Hi, You could try redefining "[", so that if any element subsetted returned an NA, it would throw an error, e.g.: (Warning: Largely untested! - this will almost certainly cause problems in other classes that use [ to subset. Possibly defining this as "[.default" would be better...) "[" <- function(x, ...) { res <- (base::"[")(x, ...) if ( any(is.na(res)) ) stop("An element was NA in a subset") res }> x <- 1:5 > x[4][1] 4> x[7]Error in x[7] : An element was NA in a subset However, you'll probably find this is a little over-zealous, e.g.:> y <- c(1:3, NA, 4) > y[5][1] 4> y[4]Error in y[4] : An element was NA in a subset If you just want to check for an NA at printing, defining a function like this might be more appropriate: print.or.stop <- function(x) { if ( any(is.na(x)) ) stop("An element was NA in a subset") print(x) } You could write a more complicated "[" function that does a bunch of testing, to see if the element extracted is going to be out of the extent of the vector (rather than a "genuine" NA), but since there are a number of ways elements can be extracted from vectors (numeric, logical and character indices can all be used to index vectors, and these have recycling rules, etc), this is probably much more work than a few checks in your code where an NA would actually indicate an error. Cheers, Rich On 4/14/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. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Rich FitzJohn rich.fitzjohn <at> gmail.com | http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike
Tony Plate
2005-Apr-13 23:21 UTC
[R] terminate R program when trying to access out-of-bounds array element?
One way could be to make a special class with an indexing method that checks for out-of-bounds numeric indices. Here's an example for vectors: > setOldClass(c("oobcvec")) > x <- 1:3 > class(x) <- "oobcvec" > x [1] 1 2 3 attr(,"class") [1] "oobcvec" > "[.oobcvec" <- function(x, ..., drop=T) { + if (!missing(..1) && is.numeric(..1) && any(is.na(..1) | ..1 < 1 | ..1 > length(x))) + stop("numeric vector out of range") + NextMethod("[") + } > x[2:3] [1] 2 3 > x[2:4] Error in "[.oobcvec"(x, 2:4) : numeric vector out of range > Then, for vectors for which you want out-of-bounds checks done when they indexed, set the class to "oobcvec". This should work for simple vectors (I checked, and it works if the vectors have names). If you want this write a method like this for indexing matrices, you can use ..1 and ..2 to refer to the i and j indices. If you want to also be able to check for missing character indices, you'll just need to add more code. Note that the above example disallows 0 and negative indices, which may or may not be what you want. If you're extensively using other classes that you've defined, and you want out-of-bounds checking for them, then you need to integrate the checks into the subsetting methods for those classes -- you can't just use the above approach. hope this helps, Tony Plate Vivek Rao 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. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Gabor Grothendieck
2005-Apr-14 01:24 UTC
[R] terminate R program when trying to access out-of-bounds array element?
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.