How to Fold Messages and Warnings in knitr

Original link:


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

 --- 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:
This site is only for collection, and the copyright belongs to the original author.