R 中的 print() 命令可以静音吗? [英] Can the print() command in R be quieted?

查看:57
本文介绍了R 中的 print() 命令可以静音吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中的一些函数可以打印信息和返回值,打印可以静音吗?

In R some functions can print information and return values, can the print be silenced?

例如:

print.and.return <- function() {
  print("foo")
  return("bar")
}

返回

> print.and.return()
[1] "foo"
[1] "bar"
> 

我可以像这样存储退货:

I can store the return like:

> z <- print.and.return()
[1] "foo"
> z
[1] "bar"
> 

我可以禁止打印 "foo" 吗?

Can I suppress the print of "foo"?

推荐答案

您可以使用 R 的隐藏函数性质,例如通过定义函数

You may use hidden functional nature of R, for instance by defining function

deprintize<-function(f){
 return(function(...) {capture.output(w<-f(...));return(w);});
}

这会将打印"功能转换为静默"功能:

that will convert 'printing' functions to 'silent' ones:

noisyf<-function(x){
 print("BOO!");
 sin(x);
}

noisyf(7)
deprintize(noisyf)(7)
deprintize(noisyf)->silentf;silentf(7)

这篇关于R 中的 print() 命令可以静音吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆