19

The function callmultmoments computes moments of the normal distribution. The function automatically prints "Sum of powers is odd. Moment is 0." if the sume of the powers is odd. Is there any way to supress that under the condition that the original function should stay untouched.

Ex:

require(symmoments)
# Compute the moment for the 4-dimensional moment c(1,1,3,4):

m.1134 <- callmultmoments(c(1,1,3,4))

EDIT:

As described here we can use

## Windows
sink("nul") 
...
sink()

## UNIX
sink("/dev/null")    # now suppresses
....                 # do stuff
sink()               # to undo prior suppression, back to normal now

However, I am writing a package so I want it to be platform independent. Any ideas what to do instead?

Manuel R
  • 3,976
  • 4
  • 28
  • 41

3 Answers3

28

The issue is due to the fact that the function has multiple print statements, where stop, warning, or message would have been appropriate so that people can use suppressWarnings or suppressMessages.

You can work arount it using invisible(capture.output()) around your whole assignment (not just the right side).

f1 <- function(n, ...){
    print("Random print statement")
    cat("Random cat statement\n")
    rnorm(n = n, ...)
}

f1(2)
#> [1] "Random print statement"
#> Random cat statement
#> [1] -0.1115004 -1.0830523
invisible(capture.output(x <- f1(2)))
x
#> [1]  0.0464493 -0.1453540

See also suppress messages displayed by "print" instead of "message" or "warning" in R.

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
hplieninger
  • 3,214
  • 27
  • 32
  • 1
    Thanks for this answer @hplieninger. Could you elaborate why we need `invisible(...)` around the `capture.output` call? I have tried it with and without in a loop, and there is no obvious difference. Just trying to work out what they are doing, and the `?` files aren't clear in this situation. Thanks. – EcologyTom Mar 23 '18 at 09:58
  • Honestly, I don't know. It's a hack, and it may well be possible that I got it from one of the [comments](https://stackoverflow.com/questions/8797314/suppress-messages-displayed-by-print-instead-of-message-or-warning-in-r#comment10975639_8797387) in the linked thread. But without `invisible()`, the `print()` and `cat()` statements above will not be suppressed. – hplieninger Mar 23 '18 at 10:12
  • Hm, that's why I ask; in my loop `print()` and `cat()` are suppressed regardless of whether I use `invisible` or not. – EcologyTom Mar 23 '18 at 10:47
0

If the function you are using is Barnard::barnard.test the following works:

invisible(capture.output(abc <- barnard.test(8,14,1,3)))

But if you use just the invisible or just the capture.output functions, you still have the unwanted printing.

Warning: if you do it the way I suggest, do not later print abc. You will get a lot of stuff you don't want. Rather, just grab the parts you really want, for example abc$p.value.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
-1

This message from callmultmoments can be suppressed by simply avoiding a moment that is not even. Any odd central moment, such as c(1,1,3,4) as in your example will have an expected value of 0 mathematically. That is, the expected value of a CENTRAL moment such as E[X^1 Y^1 Z^3 W^4], where the sum of powers, such as 1+1+3+4, is odd is automatically 0.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • Yes I am aware of that. But in my function many moments get calculated and I don’t know beforehand if they are even or odd. If you ever get around to updating the package consider changing the way the „warning“ is generated from a print statement to a proper warning statement using warning(). Like that suppressing it is much easier. – Manuel R Nov 11 '18 at 09:24
  • Thanks. I'll keep it in mind, although at this point I don't have plans for a revision. – Kem Phillips Nov 12 '18 at 12:04