[英]Suppress output of a function
I'm looking to suppress the output of one command (in this case, the apply
function).我希望抑制一个命令的 output(在本例中为
apply
函数)。
Is it possible to do this without using sink()
?是否可以在不使用
sink()
下执行此操作? I've found the described solution below, but would like to do this in one line if possible.我找到了下面描述的解决方案,但如果可能的话,我想在一行中完成。
It isn't clear why you want to do this without sink
, but you can wrap any commands in the invisible()
function and it will suppress the output.目前尚不清楚为什么要在没有
sink
的情况下执行此操作,但是您可以将任何命令包装在invisible()
函数中,它会抑制输出。 For instance:例如:
1:10 # prints output
invisible(1:10) # hides it
Otherwise, you can always combine things into one line with a semicolon and parentheses:否则,您始终可以使用分号和括号将内容组合成一行:
{ sink("/dev/null"); ....; sink(); }
Use the capture.output()
function.使用
capture.output()
函数。 It works very much like a one-off sink()
and unlike invisible()
, it can suppress more than just print messages.它的工作原理非常像一次性
sink()
并且与invisible()
不同,它可以抑制的不仅仅是打印消息。 Set the file argument to /dev/null
on UNIX or NUL
on windows.将文件参数设置为 UNIX 上的
/dev/null
或 Windows 上的NUL
。 For example, considering Dirk's note:例如,考虑 Dirk 的注释:
> invisible(cat("Hi\n"))
Hi
> capture.output( cat("Hi\n"), file='NUL')
>
The following function should do what you want exactly:以下功能应该完全符合您的要求:
hush=function(code){
sink("NUL") # use /dev/null in UNIX
tmp = code
sink()
return(tmp)
}
R 只会自动打印未分配表达式的输出,因此只需将apply<\/code>的结果分配给一个变量,它就不会被打印出来。
"
you can use 'capture.output' like below.您可以使用如下所示的“capture.output”。 This allows you to use the data later:
这允许您稍后使用数据:
log <- capture.output({
test <- CensReg.SMN(cc=cc,x=x,y=y, nu=NULL, type="Normal")
})
test$betas
In case anyone's arriving here looking for a solution applicable to RMarkdown, this will suppress all output:如果有人来到这里寻找适用于 RMarkdown 的解决方案,这将抑制所有输出:
```{r error=FALSE, warning=FALSE, message=FALSE}
invisible({capture.output({
# Your code goes here
2 * 2
# etc
# etc
})})
```
The code will run, but the output will not be printed to the HTML document代码将运行,但输出不会打印到 HTML 文档
Making Hadley's comment to an answer (hope to make it better visible).使哈德利对答案的评论(希望使它更好地可见)。 Use of apply family without printing is possible with use of the
plyr<\/code> package
使用
plyr<\/code>包可以在不打印的情况下使用 apply 系列
x <- 1:2
lapply(x, function(x) x + 1)
#> [[1]]
#> [1] 2
#>
#> [[2]]
#> [1] 3
plyr::l_ply(x, function(x) x + 1)
invisible(cat("Dataset: ", dataset, fill = TRUE))
invisible(cat(" Width: " ,width, fill = TRUE))
invisible(cat(" Bin1: " ,bin1interval, fill = TRUE))
invisible(cat(" Bin2: " ,bin2interval, fill = TRUE))
invisible(cat(" Bin3: " ,bin3interval, fill = TRUE))
produces output without NULL at the end of the line or on the next line在行尾或下一行产生不带 NULL 的输出
Dataset: 17 19 26 29 31 32 34 45 47 51 52 59 60 62 63
Width: 15.33333
Bin1: 17 32.33333
Bin2: 32.33333 47.66667
Bin3: 47.66667 63
If you're wondering how to suppress a warning()
you can use suppressWarnings()
like so:如果您想知道如何抑制
warning()
您可以像这样使用suppressWarnings()
:
suppressWarnings(warning("hi"))
Whereas these two will still show the warning:而这两个仍然会显示警告:
invisible(warning("Hi"))
# shows 'Hi'
capture.output(warning("Hi"), file='NUL')
# shows 'Hi'
for the return(something) part inside an R function:对于 R function 中的 return(something) 部分:
return(invisible(something))
works ok工作正常
invisible(return(something))
does not work at all根本不起作用
Here is a version that is robust to errors in the code to be shushed:这是一个对要隐藏的代码中的错误具有鲁棒性的版本:
quietly <- function(x) {
sink("/dev/null") # on Windows (?) instead use `sink("NUL")`
tryCatch(suppressMessages(x), finally = sink())
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.