12.3 Scrollable text output

In Section 7.4, we showed how to restrict the heights of code blocks and text output blocks via CSS. In fact, there is a simpler method with the chunk options attr.source and attr.output to add the style attribute to the fenced code blocks in the Markdown output (see Section 11.13 for more information on these options). For example, for this code chunk with the attr.output option:

  1. ```{r, attr.output='style="max-height: 100px;"'}
  2. 1:300
  3. ```

Its Markdown output will be:

  1. ```r
  2. 1:300
  3. ```
  4. ```{style="max-height: 100px;"}
  5. ## [1] 1 2 3 4 5 6 7 8 9 10
  6. ## [11] 11 12 13 14 15 16 17 18 19 20
  7. ## ... ...
  8. ```

Then the text output block will be converted to HTML by Pandoc:

  1. <pre style="max-height: 100px;">
  2. <code>## [1] 1 2 3 4 5 6 7 8 9 10
  3. ## [11] 11 12 13 14 15 16 17 18 19 20
  4. ## ... ...</code>
  5. </pre>

To learn more about Pandoc’s fenced code blocks, please read its manual at https://pandoc.org/MANUAL.html#fenced-code-blocks.

The attr.source and attr.output options have made it possible for us to specify maximum heights for individual code chunks. However, the syntax is a little clunky, and requires a better understanding of CSS and Pandoc’s Markdown syntax. Below we show an example of a custom output hook that works with a custom chunk option max.height, so you will only need to set the chunk option like max.height = "100px" instead of attr.output = 'style="max-height: 100px;"'. In this example, we only manipulate the options argument, but not the x argument.

  1. ---
  2. title: Scrollable code blocks
  3. output:
  4. html_document:
  5. highlight: tango
  6. ---
  7. We set up an `output` hook to add a `style` attribute to the
  8. text output when the chunk option `max.height` is set.
  9. ```{r, include=FALSE}
  10. options(width = 60)
  11. local({
  12. hook_output <- knitr::knit_hooks$get('output')
  13. knitr::knit_hooks$set(output = function(x, options) {
  14. if (!is.null(options$max.height)) options$attr.output <- c(
  15. options$attr.output,
  16. sprintf('style="max-height: %s;"', options$max.height)
  17. )
  18. hook_output(x, options)
  19. })
  20. })
  21. ```
  22. Without the `max.height` option, you will see the full output,
  23. e.g.,
  24. ```{r}
  25. 1:100
  26. ```
  27. Now we set `max.height` to `100px`. You will see a scrollbar
  28. in the text output because its height is larger than 100px.
  29. ```{r, max.height='100px'}
  30. 1:100
  31. ```
  32. Essentially the `max.height` option is converted to the
  33. `attr.output` option. It works even if the `attr.output`
  34. option is present, i.e., it will not override the
  35. `attr.output` option, e.g., we show line numbers on the left
  36. side of the text output via the `.numberLines` attribute:
  37. ```{r, max.height='100px', attr.output='.numberLines'}
  38. 1:100
  39. ```

Figure 12.1 shows the output. Note that in the last code chunk with the chunk option attr.output, the option will not be overridden by max.height because we respect existing attributes by combining them with the style attribute generated by max.height:

  1. options$attr.output <- c(
  2. options$attr.output,
  3. sprintf('style="max-height: %s;"', options$max.height)
  4. )

An example of scrollable text output, with its height specified in the chunk option max.height.

FIGURE 12.1: An example of scrollable text output, with its height specified in the chunk option max.height.

You can use a similar trick in the source hook to limit the height of source code blocks.