该函数callmultmoments计算正态分布的矩。"Sum of powers is odd. Moment is 0."如果幂的和为奇数,该功能将自动打印。在保持原始功能不变的情况下,有什么方法可以抑制这种情况。
例如:
require(symmoments)
# Compute the moment for the 4-dimensional moment c(1,1,3,4):
m.1134 <- callmultmoments(c(1,1,3,4))
Run Code Online (Sandbox Code Playgroud)
编辑:
如此处所述,我们可以使用
## Windows
sink("nul")
...
sink()
## UNIX
sink("/dev/null") # now suppresses
.... # do stuff
sink() # to undo prior suppression, back to normal now
Run Code Online (Sandbox Code Playgroud)
但是,我正在编写一个程序包,因此我希望它与平台无关。有什么想法可以代替吗?
这个问题是由于该函数有多个print语句,其中stop,warning或message将是适当的,这样人们可以使用suppressWarnings或suppressMessages。
您可以使用来启动它invisible(capture.output())。
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
Run Code Online (Sandbox Code Playgroud)
另请参阅禁止在R中使用“打印”而不是“消息”或“警告”显示消息。
| 归档时间: |
|
| 查看次数: |
2584 次 |
| 最近记录: |