Suppress super long output

First we redefine the output hook:

library(knitr)
# the default output hook
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
  if (!is.null(n <- options$out.lines)) {
    x = knitr:::split_lines(x)
    if (length(x) > n) {
      # truncate the output
      x = c(head(x, n), '....\n')
    }
    x = paste(x, collapse = '\n') # paste first n lines together
  }
  hook_output(x, options)
})

And we do not want the output to be more than 4 lines, so we set this option globally:

opts_chunk$set(out.lines = 4)

Test the new output hook:

1+1 # this is short

The output of this chunk is truncated:

1:100


Try the parsermd package in your browser

Any scripts or data that you put into this service are public.

parsermd documentation built on May 20, 2021, 5:08 p.m.