让函数静默返回 [英] Make a function return silently

查看:12
本文介绍了让函数静默返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个静默返回的 R 函数,例如我从 barplot 函数中得到的.

I would like to write an R function that returns silently, like what I get from the barplot function for example.

我的意思是,如果我执行 output = myfunction(),我可以将输出存储在变量中,但是如果我只使用 myfunction(),则不会打印此输出>.

I mean that I can store an output in a variable if I do output = myfunction(), but this output does not get printed if I just use myfunction().

推荐答案

myFunc <- function(x){
  invisible(x*2)
}

> myFunc(4)
> y <-myFunc(4)
> y
[1] 8
> 

这篇关于让函数静默返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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