在Knitr/Rmarkdown中抑制消息 [英] Suppressing messages in Knitr / Rmarkdown

查看:117
本文介绍了在Knitr/Rmarkdown中抑制消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的RMarkdown文件的代码:

Here is the code for my RMarkdown file:

```{r echo=FALSE, message=FALSE}
opts_chunk$set(comment = NA, echo=FALSE, message = FALSE, warnings = FALSE)
options("getSymbols.warning4.0"=FALSE)
Sys.setenv(TZ = "GMT")
library(quantmod)
library(xtable)
library(PerformanceAnalytics)
```

```{r}
getSymbols("^RUT")
chart.TimeSeries(RUT)
dev.off()
```

尽管设置为message = FALSE, warnings = FALSE,但运行getSymbols()dev.off()时仍在HTML文件中获取输出消息.它们各自的输出是:

Despite settings message = FALSE, warnings = FALSE, I am still getting output messages in the HTML file when I run getSymbols() and dev.off(). Their respective outputs are:

[1] "RUT"

null device 
          1 

如何隐藏这些消息?

推荐答案

尝试使用invisible禁止显示这些类型的输出.

Try using invisible to suppress those types of output.

```{r}
invisible(getSymbols("^RUT"))
chart.TimeSeries(RUT)
invisible(dev.off())
```

?invisible的帮助页面:

当需要具有可以分配的返回值但未分配时不打印的函数时,此函数很有用.

This function can be useful when it is desired to have functions return values which can be assigned, but which do not print when they are not assigned.

这些都不是消息"或警告",而是实际输出值.您会看到消息确实被输出中的knitr抑制了.

Neither of these are "messages" or "warnings", but actual output values. You'll see that the messages for getSymbols are, indeed, suppressed by knitr in the output.

这篇关于在Knitr/Rmarkdown中抑制消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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