Skip to content

terminate R program when trying to access out-of-bounds a rray element?

1 message · Liaw, Andy

#
As Bert said, redefining functions like "[" is surely inadvisable, because
of possibility of breaking codes that depend on the intended behavior.  This
is a language _feature_.

If the problem is indexing beyond array extent, just check for it:  Are any
values that are going to be used for indexing larger than the length of the
object?  E.g.,

if (any(idx > length(x))) stop("index out of bound")
result <- x[i]

If this is still too much work for you, Perhaps R is not for you...

Andy