Suppress automatic output to console in R

后端 未结 2 511
不思量自难忘°
不思量自难忘° 2021-01-04 19:40

The function callmultmoments computes moments of the normal distribution. The function automatically prints \"Sum of powers is odd. Moment is 0.\"

相关标签:
2条回答
  • 2021-01-04 20:00

    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.

    0 讨论(0)
  • 2021-01-04 20:12

    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.

    0 讨论(0)
提交回复
热议问题