简体   繁体   English

将R编织为PDF时限制消息输出

[英]Limit message output when knitting R to PDF

I have a function that reports progress via message() . 我有一个通过message()报告进度的函数。 Under normal use, this is desirable, and the messages can be suppressed via suppressMessages() if desired. 在正常使用情况下,这是可取的,如果需要,可以通过suppressMessages()来抑制消息。

However, I'm writing a vignette (in Rmarkdown) that calls this function, and the results include a full page of these progress updates. 但是,我正在编写一个调用此函数的插图(在Rmarkdown中),结果包括这些进度更新的完整页面。 I'd like to include the first few lines of messages, but not waste a whole page on them. 我想包括前几行消息,但不要浪费整整一页。 Is there a way to configure this? 有没有办法配置这个?

I've tried passing the chunk option R.options=list(max.print=10) , as suggested in another answer here , but that doesn't appear to apply to messages. 我已经尝试传递块选项R.options=list(max.print=10)如此处的另一个答案所示 ,但这似乎不适用于消息。 Or at least, not in this context, where the actual messages are generated one or two at a time within each function, but that function is called as part of a loop. 或者至少,不是在这种情况下,在每个函数内一次生成一个或两个实际消息,但该函数被称为循环的一部分。

The general structure I'm working with is: 我正在使用的一般结构是:

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)
}

The markdown chunk looks like this: 降价块看起来像这样:

```{r batchFlowHist, R.options=list(max.print=10)}
batch1 <- function1(flowPloidyFiles)
```

I'd like my vignette to display the messages from the first few files that are processed, but not the entire loop. 我希望我的插图能够显示处理的前几个文件中的消息,而不是整个循环。 Is there some way to do this? 有办法做到这一点吗?

Turns out this is already supported in knitr via the message argument. 事实证明,knitr已经通过message参数支持了这一点。 The linked answer suggested it didn't work, but since that was posted it must have been added. 链接的答案表明它不起作用,但由于发布了它必须已添加。 So the following solves my problem: 所以以下解决了我的问题:

```{r batchFlowHist, message = 1:10}
batch1 <- function1(flowPloidyFiles)
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM