43

I would like to be able to specify multiple output formats at the same time, for instance html_document and a pdf_document. I know that this can be done very simply with something like

---
  output: [html_document, pdf_document]
---

I might have some of that syntax off, but I can not seem to find the documentation anywhere. I have recently discovered knitr-bootstrap and love it. It is what I have been looking for to be able to dynamically hide my code and output blocks.

Unfortunately, by default, the YAML block for the knitr-bootstrap invocation is quite complex and I do not know how to specify multiple outputs for this.

I have looked at the YAML spec and tried a few different things but I am at a loss. Below is my current YAML frontmatter.

---
title: "Beta Regression Comparison"
opset: bootstrap
output:
  knitrBootstrap::bootstrap_document:
    title: "Beta Regression Comparison"
    theme: Simplex
    highlight: Solarized - Light
    theme.chooser: FALSE
    highlight.chooser: FALSE
    menu: FALSE
  pdf_document
---
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Justace Clutter
  • 2,097
  • 3
  • 18
  • 31
  • This Quarto documentation provides good examples for what you trying to do: https://quarto.org/docs/get-started/authoring/rstudio.html – StatsStudent Feb 22 '23 at 20:06

2 Answers2

45

The solution is to change pdf_document to pdf_document: default. I can't unfortunately find a reference for this syntax in the official documentation. If however you open a RMarkdown document in a recent version of RStudio, click Knit HTML and then Knit PDF, it uses this : default syntax.

The syntax is:

---
output:
  html_document:
    keep_md: yes
  pdf_document: default
---
Shaun Jackman
  • 956
  • 10
  • 15
  • 1
    Its just a limitation of yaml - output needs to be a named list, and you can't create a named element with no content – hadley Mar 05 '15 at 12:10
  • Yes, true. I wasn't able though to find a reference or example for the `default` keyword in the `rmarkdown` documentation. – Shaun Jackman Mar 06 '15 at 16:00
  • thanks, I find have been looking for this. I don't find the knitr header to be so intuitive. – OfficialBenWhite Jul 26 '15 at 23:14
  • 4
    The flag for keep_md is no longer "yes" but "true". (so: `keep_md: true`). See the documentation: https://bookdown.org/yihui/rmarkdown/html-document.html – FvD Aug 24 '18 at 16:48
4

In my case, I tried to knit multiple output-documents using bookdown and found this post which allowed me to get the desired result.

You can write the output-definition in your YAML header as follows:

---
output:
  bookdown::pdf_document2:
    template: "path-to-my-template"
  bookdown::word_document2:
    default
knit: (function(inputFile, encoding){
  rmarkdown::render(inputFile, encoding = encoding,
  output_dir = "my-output-path", output_format = "all") })
---
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
bt-koch
  • 59
  • 5