How to Fold Messages and Warnings in knitr

Original link: https://yihui.org/en/2023/05/fold-messages/

logo.png

Most R Markdown users may know that code blocks can be
folded
in HTML
output. Then @rkb965 asked a good
question
on how to fold messages
and warnings. The answer was not clear to me in the beginning, but later I
recalled a contribution by
@atusy
and came up with this
solution:

 --- title: "Test" output: html_document: code_folding: show --- ```{r, setup, include=FALSE} knitr::opts_chunk$set( class.message = 'foldable fold-hide', class.warning = 'foldable fold-hide' ) ``` ```{r} message('Hello message!') warning('Hello warning!') ```

Basically if an output block (not necessary a source code block) has the class
foldable and the code_folding feature is enabled, the block will be
foldable. To apply this class to messages and warnings, we just need to set the
chunk options class.message and class.warning , respectively. The class
fold-hide in the above example means messages and warnings will be hidden
initially. They could be unfolded if you click the button on the top right.

This article is transferred from: https://yihui.org/en/2023/05/fold-messages/
This site is only for collection, and the copyright belongs to the original author.