0

This is an update to an old question, How to suppress warnings globally in an R Script

where the solution was like using

    warn <- options(warn=-1)
    on.exit(options(warn))

But now CRAN says that options(warn=-1) is not allowed and says to use suppressWarnings() instead. My package was rejected because of this. But that doesn't work globally. What can be done?

user101089
  • 3,756
  • 1
  • 26
  • 53
  • 2
    I think this (i.e. globally changing warning behaviour when your package is loaded) is simply against CRAN rules (this would probably fall under " Packages should not modify the global environment (user’s workspace). "). Can you say more about your use case to help us help you? Why are you doing this? Do you want to suppress warnings *emanating from your package functions*? – Ben Bolker Apr 19 '22 at 15:08
  • 1
    PS I agree that the solution you've posted is reasonable (it will suppress warnings only for the duration of the function call), but if CRAN doesn't like it, you'll have to find another way to achieve what you want. It will probably be harder to implement/we need more information about your use case and goals. – Ben Bolker Apr 19 '22 at 15:46
  • Where exactly did you put those two lines of code? Did you put them in a function? Or did you have them as a top-level statement in your package? – MrFlick Apr 19 '22 at 15:55
  • These lines of code are in a function, that calls `chol()` for a Cholesky decomposition in a context where I need to allow for the fact that the matrix may be singular. This is used in my {gellipsoid} package, https://github.com/friendly/gellipsoid – user101089 Apr 19 '22 at 22:20

2 Answers2

2

You could put the entire body of your function inside a suppressWarnings() block, e.g.

foo <- function(a,b,c) {
  ret_values <- suppressWarnings({
    ## body of the function goes here
  })
  return(ret_values)
}

This is is a hack (among other things, it will make source-level debugging harder), the original options()/on.exit(options(...)) solution is better, but if CRAN doesn't like it you're stuck.

If you just want to prevent a particular function call from issuing a warning (according to your comments above, it's chol() in your case), then suppressWarnings(chol(...)) should work, and should be better than the brute-force solution suggested above (based on this commit it looks like you've already implemented this ...)

It would be even better to be able to trap only specific warnings: e.g. sqrt(-1) and sqrt(10000000000000000000000L) return different warnings, one might want to trap the "NaNs produced" warning and not the "non-integer value qualified with L" warning. Unfortunately for reasons explained on the r-devel mailing list in 2012 (i.e., warning messages may be translated so you can't use text-matching on the message), there's (AFAIK) no reliable way to do this.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • OK, this is a general solution, but IMHO far to draconian. My solution is to just trap the call to `chol()`. – user101089 Apr 19 '22 at 22:26
  • I agree. But since you didn't provide your use case (i.e. I didn't know there was a single point at which you were trying to trap warnings), I gave the general solution. – Ben Bolker Apr 19 '22 at 22:29
0

In my use case, the solution is just to trap the call to chol() that will give warnings.

ellipsoid <- function(center, 
                      shape, 
                      radius=1, 
                      segments=60, 
                      warn.rank=FALSE){

    # adapted from the shapes3d demo in the rgl package and from the Rcmdr package
    degvec <- seq(0, 2*pi, length=segments)
    ecoord2 <- function(p) c(cos(p[1])*sin(p[2]), 
                             sin(p[1])*sin(p[2]), 
                             cos(p[2]))
    v <- t(apply(expand.grid(degvec,degvec), 1, ecoord2))
    # if (!warn.rank){
    #   warn <- options(warn=-1)
    #   on.exit(options(warn))
    # }
    if (warn.rank) {
    Q <- chol(shape, pivot=TRUE)
    }
    else {
      Q <- suppressWarnings(chol(shape, pivot=TRUE))
    }
    order <- order(attr(Q, "pivot"))
    v <- center + radius * t(v %*% Q[, order])
    v <- rbind(v, rep(1,ncol(v))) 
    e <- expand.grid(1:(segments-1), 1:segments)
    i1 <- apply(e, 1, function(z) z[1] + segments*(z[2] - 1))
    i2 <- i1 + 1
    i3 <- (i1 + segments - 1) %% segments^2 + 1
    i4 <- (i2 + segments - 1) %% segments^2 + 1
    i <- rbind(i1, i2, i4, i3)
    x <- asEuclidean(t(v))
    ellips <- qmesh3d(v, i)
    return(ellips)
}
user101089
  • 3,756
  • 1
  • 26
  • 53