我有一个通过 message()
报告进度的功能.在正常使用情况下,这是可取的,并且可以通过 suppressMessages()
抑制消息。如果需要。
但是,我正在编写一个调用此函数的小插图(在 Rmarkdown 中),结果包括这些进度更新的完整页面。我想包括前几行消息,但不要在它们上面浪费一整页。有没有办法配置这个?
我试过传递块选项 R.options=list(max.print=10)
, as suggested in another answer here ,但这似乎不适用于消息。或者至少,不是在这种情况下,在每个函数中一次生成一两个实际消息,但该函数作为循环的一部分被调用。
我正在使用的一般结构是:
function1 <- function(file.list){
res <- list()
for(i in seq_along(file.list)){
message("processing ", file.list[i])
res[i] <- function2(file.list[i])
}
}
function2 <- function(file){
message("analyzing ", file)
tryVal <- try(res <- analysisFunction(file), silent = TRUE)
if(inherits(tryVal, "try-error")){
message("*** problem with ", file, " ***")
} else {
## additional processing
}
return(res)
}
Markdown 块看起来像这样:
```{r batchFlowHist, R.options=list(max.print=10)}
batch1 <- function1(flowPloidyFiles)
```
我希望我的小插图显示来自处理的前几个文件的消息,而不是整个循环。有没有办法做到这一点?
最佳答案
事实证明,knitr 已经通过 message
支持了这一点。争论。链接的答案表明它不起作用,但既然已经发布,它必须已添加。所以以下解决了我的问题:
```{r batchFlowHist, message = 1:10}
batch1 <- function1(flowPloidyFiles)
```
关于r - 将 R 编织为 PDF 时限制消息输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42838568/