0

I wanted to have a dynamic .qmd report which included some mapview plots, but the code would not compile if NOT in HTML format, because it would complain about code generating HTML code for non HTML outputs. Nonetheless I wanted to optionally generate docx or .pdf, depending on the objective, form the same quarto document. How could this be done?

1 Answer 1

0

It took me a while to google an answer that worked for me, with folks point mostly to here:

https://quarto.org/docs/authoring/conditional.html

those solutions involved .content-visible and .content-hidden classes, but those only mean the code will be shown or not depending on the output, but the code will always be evaluated, generating errors the same if not in HTML and HTML is produced. Since it took me a while to get this, I am sharing it here as a Q with and A by myself, in case it is helpful for others.

This is the template of code I used and it worked nicely:

if (knitr::is_html_output()) {
  # here goes code that will run if output HTML
  plot(rnorm(50),rnorm(50))
} else {
  # here goes code that will run if output is NOT HTML
  print("Plot only shown if output is pdf")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.