It may be helpful for debugging if the two conditions here were separated:
if( n < 1 || n > NROW(x) ) stop("Invalid 'n'")
For example:
if( n < 1 ) stop("n, Number of rows of data, 0 or negative.")
# If possible, give the actual n.
if( n > NROW(X) ) stop("Requested n greater than the length of provided data.")
# If possible give the actual n, and the comparison NROW(x), and x.
It may be helpful for debugging if the two conditions here were separated:
if( n < 1 || n > NROW(x) ) stop("Invalid 'n'")
For example: