【问题标题】:Limit message output when knitting R to PDF将 R 编织为 PDF 时限制消息输出
【发布时间】:2017-03-16 15:38:44
【问题描述】:

我有一个通过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)
```

我希望我的小插图显示来自已处理的前几个文件的消息,但不是整个循环。有没有办法做到这一点?

【问题讨论】:

    标签: r markdown knitr


    【解决方案1】:

    事实证明这已经通过 message 参数在 knitr 中得到支持。链接的答案表明它不起作用,但是自从发布以来,它必须已添加。所以以下解决了我的问题:

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

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 2020-05-29
      • 2021-04-20
      • 1970-01-01
      • 2018-04-20
      • 2016-07-11
      • 2021-06-22
      • 1970-01-01
      • 2021-12-07
      相关资源
      最近更新 更多