Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Quarto: Running different R code in dependency of the output format

I have a qmd Quarto file with different output formats, html and pdf.

My goal is, to generate graphics in dependence of the output format.

How can I detect in an R cell, if the processing output format is html or pdf?
A simple if statement would be sufficient.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---
title: My title 

format:
  html:
    toc: true
    toc-depth: 3
    html-math-method: katex
  pdf:
    keep-tex: true
    toc: true
    number-sections: true
    colorlinks: true
---

´´´

>Solution :

Using knitr::is_html_output you could do:

---
title: My title 
format:
   html:
    toc: true
    toc-depth: 3
    html-math-method: katex
  pdf:
    keep-tex: true
    toc: true
    number-sections: true
    colorlinks: true
---

```{r}
if (knitr::is_html_output()) {
  print("HTML")
} else {
  print("pdf")
}
```

Which in case of html will give:

enter image description here

and for pdf:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading