I’m writing vignettes on RStudio using Rmarkdown and knitr for an R package that I’m developing. In one chunk, I’d like to demonstrate the operation of a function I wrote and show its output. Essentially, this function is a for loop that calls many other functions within it. Each of these functions, during each cycle, prints messages using message()
and various phrases using cat()
as well as paste()
in the two.
Now, since the output would be very long, I’m looking for an elegant solution to display the code’s result. I thought about adding a scrollbar on the side so that after surpassing 100px (or whatever dimension), you can continue reading the output by scrolling.
I found this online to use in the setup chunk:
#```{r setup}
options(width = 60)
local({
hook_output <- knitr::knit_hooks$get('output')
knitr::knit_hooks$set(output = function(x, options) {
if (!is.null(options$max.height)) options$attr.output <- c(
options$attr.output,
sprintf('style="max-height: %s;"', options$max.height)
)
hook_output(x, options)
})
}))
However, the issue is that the output of my function is, as I mentioned, a collection of outputs from the various functions it calls internally as well as a mix of message()
, cat()
and paste()
. Therefore, each internal function seems to reset the output count. I’ve also tried using collapse=TRUE
and results='hold'
in the chunk option, but the former, aside from putting all the output together, doesn’t seem to affect the counting, so the output still appears fragmented. The latter separates messages from cat outputs but still doesn’t solve my problem.
I’ve also tried other functions to truncate the output directly after a certain number of lines, like this one:
#```{r setup}
hook_output <- knitr::knit_hooks$get("output")
# set a new output hook to truncate text output
knitr::knit_hooks$set(output = function(x, options) {
if (!is.null(n <- options$out.lines)) {
x <- xfun::split_lines(x)
if (length(x) > n) {
# truncate the output
x <- c(head(x, n), "....n")
}
x <- paste(x, collapse = "n")
}
hook_output(x, options)
})
and using out.lines = 30
in the chunk options. However, it seems like it can’t count the output of the chunk as a single element, so it does truncate but only some parts of the text, then starts over.
I’ve also tried with some other methods like using R.options()
```{r R.options=list(max.print=10)}
a <- my_func(...)
but the result has again been the same. Or by using capture.output()
but it reached memory limits since the output is very long, so i don’t know if it could work somehow.
My setup chunk for now is this one:
```{r setup}
library(knitr)
library(htmltools)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
A very short example of my output is something like this:
#> Computing layer 1 of cross-validation
#> Importing specified datasets...
#> Splitting datasets in train-test
#> Pre-computing feature selection A
#> Kept X features out of Y. Number of removed features X
#> Pre-computing feature selection B
#> parameter1: x
#> parameter2: y
#> Kept X features out of Y. Number of removed features X
#> Pre-computing feature selection C
#> Kept X features out of Y. Number of removed features X
#> Selecting the best ones...
#>
#> **Starting classification tasks**
#>
#> Applying feature selection A before method1
#> Fitting method1
#> Algorithm parameters: tL = a, n = b, r = c
#> Accuracy: 0.84
#> Precision: 0.7
#> Sensitivity: 0.88
#> Accomplished in 9.45 secs
#>
#> Applying feature selection C before method2
#> Fitting method2
#> Algorithm parameters: tL = a, n = b, r = c
#> Accuracy: 0.8
#> Precision: 0.8
#> Sensitivity: 0.75
#> Accomplished in 38.57 secs
#>
#> This for many other methods and many other cross-val layers ...
My desired output is the same long output with a scrollbar on the side, or a truncated version of my output after a certain number of lines.
Giomu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.