R
Quarto
Published

June 20, 2023

Conditional R cells

For R notebooks in Quarto not all information that can be presented in an html-document is feasible for the respective pdf-document. So sometimes, I want to include information in html but not in the pdf.

Interactive data tables with reactable are an example. They allow to explore smaller data sets quickly within a notebook, but work only in html documents. A pdf simply fails to render with Quarto.

So I was looking for a solution to execute R code in a notebook only for the html-document but not the pdf.

Conditional chunk execution is not implemented in Quarto so far — see ticket #3260.

However, for R I found an approach that is working well.

if (knitr::is_html_output()) {
  print("code block run")
}
[1] "code block run"
if (!knitr::is_html_output()) {
  print("code block not run")
}

An example for a notebook with a dynamic table in a html document and a static table in the pdf document