在R中,某些功能可以打印信息和返回值,打印是否可以被静音?
例如:
print.and.return <- function() {
print("foo")
return("bar")
}
Run Code Online (Sandbox Code Playgroud)
回报
> print.and.return()
[1] "foo"
[1] "bar"
>
Run Code Online (Sandbox Code Playgroud)
我可以存储退货像:
> z <- print.and.return()
[1] "foo"
> z
[1] "bar"
>
Run Code Online (Sandbox Code Playgroud)
我可以抑制打印"foo"吗?
您可以使用R的隐藏功能特性,例如通过定义函数
deprintize<-function(f){
return(function(...) {capture.output(w<-f(...));return(w);});
}
Run Code Online (Sandbox Code Playgroud)
这会将'打印'功能转换为'静音'功能:
noisyf<-function(x){
print("BOO!");
sin(x);
}
noisyf(7)
deprintize(noisyf)(7)
deprintize(noisyf)->silentf;silentf(7)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4527 次 |
| 最近记录: |