Tyler
Tyler

Reputation: 10032

Limit message output when knitting R to PDF

I have a function that reports progress via message(). Under normal use, this is desirable, and the messages can be suppressed via suppressMessages() if desired.

However, I'm writing a vignette (in Rmarkdown) that calls this function, and the results include a full page of these progress updates. 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. 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?

Upvotes: 4

Views: 718

Answers (1)

Tyler
Tyler

Reputation: 10032

Turns out this is already supported in knitr via the message argument. 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)
```

Upvotes: 5

Related Questions