[R-pkg-devel] error in sample(), invalid 'size' argument
Iñaki Ucar
|uc@r @end|ng |rom |edor@project@org
Wed Jan 15 15:15:56 CET 2020
On Wed, 15 Jan 2020 at 13:58, Roberts, David <droberts using montana.edu> wrote:
>
> Colleagues,
>
> I have a function (maxsimset) in package optpart that has worked for
> at least a decade, passes check --as-cran on my ubuntu-based system, and
> builds without errors at win-builder and CRAN windows, but fails at CRAN
> debian (unstable) with the error
>
> > mss.test(mss.10,shoshsite$elevation)
> Error in sample.int(length(x), size, replace, prob) :
> invalid 'size' argument
> Calls: mss.test -> sample -> sample.int
>
> an excerpt of the function is below
>
> 1 mss.test <- function(mss,env,panel='all',
> 2 main=deparse(substitute(env)),...)
> 3 {
> 4 if (class(mss) != "mss") {
Not related to the issue, but please use "inherits" instead.
> 5 stop("You must pass an object of class mss ...")
> 6 }
> 7 if (!is.numeric(env)) {
> 8 stop("You must pass only numeric vectors ... ")
> 9 }
> 10 setsiz <- ncol(mss$member)
> 11 nset <- mss$numset
> 12 null <- rep(0,nset)
> 13 for (i in 1:nset) {
> 14 tmp <- sample(1:length(env),setsiz)
> 15 nullmin <- min(env[tmp])
> 16 nullmax <- max(env[tmp])
> 17 null[i] <- nullmax - nullmin
> 18 }
> 19 . . .
> 20 . . .
> 21 . . .
> 22 }
>
> The error occurs at line 14 in the excerpt above. As you can see, the
> 'size' argument is setsiz, taken at line 10 as the ncol(mss$member); mss
> has already been checked for conformance
No, it has not. Nothing stops you from changing the contents of a S3
object, so just verifying the class is not a conformance check.
> and does indeed have a list object matrix called $member.
What do you mean by "list object matrix"?
> In my tests, setsiz has mode numeric,
> class integer, and size 1 as expected. The specific error message I
> receive can be generated from sample() if size is a list object or
> data.frame, but not a vector or matrix.
My guess is that, in some call, you got an unintended simplification
when assigning $member. For any object without a "dim" attribute,
ncol(object) is NULL. And a NULL size in sample() raises such an
error.
Iñaki
More information about the R-package-devel
mailing list