如何隐藏或禁用功能内打印的消息 [英] How to hide or disable in-function printed message

查看:36
本文介绍了如何隐藏或禁用功能内打印的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个函数,例如:

Suppose I have a function such as:

ff <- function(x) {
  cat(x, "
")
  x^2}

并通过以下方式运行它:

And run it by:

y <- ff(5)
# 5 
y
# [1] 25

我的问题是如何禁用或隐藏从 cat(x, " ") 打印的 5 如:

My question is how to disable or hide the 5 printed from cat(x, " ") such as:

y <- ff(5)
y
# [1] 25

推荐答案

你可以使用 capture.outputinvisible

> invisible(capture.output(y <- ff(2)))
> y
[1] 4

sink

> sink("file")
> y <- ff(2)
> sink()
> y
[1] 4

这篇关于如何隐藏或禁用功能内打印的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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