Collapse messages and text output and add a scrollbar to chunk output when writing vignettes in Rmd

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.

New contributor

Giomu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật