[英]Suppress messages in markdown, but not in R console
I currently use the following header: 我目前使用以下标题:
```{r, message=FALSE}
foo <- function(x) message(x)
for(i in 1:10) foo(i)
```
Inside this code chunk, there is a loop over simulated scenarios, with message()
function that prints status of currently executed scenario. 在这个代码块中,有一个模拟场景的循环,使用
message()
函数打印当前执行场景的状态。
I would like to suppress those messages from display in RStudio and final HTML output, but I still want to control the simulation progress and see the message()
output in console. 我想在RStudio和最终HTML输出中禁止显示这些消息,但我仍然希望控制模拟进度并在控制台中看到
message()
输出。 Is this achievable? 这可以实现吗? Maybe with other arguments/functions?
也许与其他参数/功能?
You can write/append status to a file (this is a workaround solution, there should be a more direct answer). 您可以在文件中写入/追加状态(这是一种解决方法,应该有更直接的答案)。
For example: 例如:
file <- file("status.txt", open = "wt")
sink(file, type = "message")
message("all good")
In this example message won't be displayed - it'll be written to a status.txt
file. 在此示例中,不会显示消息 - 它将被写入
status.txt
文件。
In you're using specific function and iterating over a set you can try this example: 在您使用特定功能并迭代一组时,您可以尝试以下示例:
foo <- function(x) {
message(x)
}
file <- file("status.txt", open = "wt")
sink(file, type = "message")
for(i in 1:3) {
foo(i)
}
Function foo
should return (message) value, however it appends it to a status.txt
file. 函数
foo
应返回(message)值,但是它会将其附加到status.txt
文件中。
You can track changes in status.txt
file using bash tail
command with -f
argument. 您可以使用带有
-f
参数的bash tail
命令跟踪status.txt
文件中的更改。 First send R to background and then use tail -f status.txt
in your console. 首先将R发送到后台,然后在控制台中使用
tail -f status.txt
。
One approach would be to put this in the start of your file. 一种方法是将其放在文件的开头。
mymessage <- function (text) {
if(knitr::opts_knit$get('out.format') != NULL) message(text)
}
There are various ways to know if you are within knitr, recent versions have knitr::is_latex_output
and similar. 有各种方法可以知道你是否在knitr中,最近的版本有
knitr::is_latex_output
和类似的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.