Skip to main content

R Markdown Cookbook Practical Tips and Tricks for R Markdown

Section 11.9 Collapse text output blocks into source blocks

If you feel there is too much spacing between text output blocks and source code blocks in the output, you may consider collapsing the text output into the source blocks with the chunk option collapse = TRUE. When collapse = TRUE, the source code and its text output will appear in a single block instead of two separate blocks. This is what the output looks like when collapse = TRUE:
1 + 1
## [1] 2
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
Below is the same chunk but it does not have the option collapse = TRUE (the default is FALSE):
1 + 1
## [1] 2
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
By default, collapse = FALSE and source code and output appear in separate blocks.