17
votes

Can I write a YAML header to produce multiple output formats for an R Markdown file using knitr? I could not reproduce the functionality described in the answer for the original question with this title.

This markdown file:

---
title: "Multiple output formats"
output: 
    pdf_document: default
    html_document:
      keep_md: yes
---

# This document should be rendered as an html file and as a pdf file

produces a pdf file but no HTML file.

And this file:

---
title: "Multiple output formats"
output: 
  html_document:
    keep_md: yes
  pdf_document: default
---

# This document should be rendered as an html file and as a pdf file

produces an HTML file (and an md file) but no pdf file.

This latter example was the solution given to the original question. I have tried knitting with Shift-Ctrl-K and with the Knit button in RStudio, as well as calling rmarkdown::render, but only a single output format is created, regardless of the method I use to generate the output file.

Possibly related, but I could not identify solutions:

Using R version 3.3.1 (2016-06-21), knitr 1.14, Rmarkdown 1.3

2
Where does the (new) in your title refer to? Is that a special version of knitr like NT in Windows NTAnthon
@Anthon the parenthetical "new" refers to the fact this question has the same title as a previously asked (and answered) question.Jeff

2 Answers

21
votes

I actually briefly mentioned in Render all vignette formats #1051 and you missed it:

rmarkdown::render('your.Rmd', output_format = 'all')

It is documented on the help page ?rmarkdown::render.

1
votes

Notwithstanding Yihui Xie's authoritative answer, and with due respect to the author of a great package, there are many cases in which output_format = 'all' is sub-optimal.
One of the issues that this solution raises is that the R script is re-processed from scratch for each format. Proof:

 rmarkdown::render("new.Rmd", output_format = c("html_document", "pdf_document"))


processing file: new.spin.Rmd
  |.......................                                               |  33%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-1
  |......................................................................| 100%
  ordinary text without R code


output file: new.knit.md

"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.html --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\fabrn\AppData\Local\Temp\RtmpW6Vban\rmarkdown-str3490247b1f1e.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 

Output created: new.html


processing file: new.spin.Rmd
  |.......................                                               |  33%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-1
  |......................................................................| 100%
  ordinary text without R code


output file: new.knit.md

"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.tex --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 

This really is an issue when it comes to processing big data.
In real-world examples, I usually use latex output as a single rmarkdown::render output, then reprocess the .tex files using pandoc or similar tools (like prince for pdf). So my workflow is like:

 rmarkdown::render('new.R', output_format = 'latex_document')
 lapply(c("html", "pdf", ...), 
        function(form) rmarkdown::pandoc_convert("new.tex", output=paste0("new.", form)))

The bottom line is: all depends on your data. If small, output_format='all' is straightforward. If big, you are better off with a common-ground format (latex is a good choice but html may be better in some cases) as an input to conversion tools.