Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying multiple simultaneous output formats in knitr

Tags:

r

knitr

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 --- 
like image 850
Justace Clutter Avatar asked Aug 01 '14 10:08

Justace Clutter


People also ask

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.

What is the functionality of knitr package?

It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming. It is licensed under the GNU General Public License.

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

What are the three types of sections in an R Markdown document?

There are three basic components of an R Markdown document: the metadata, text, and code.


2 Answers

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 --- 
like image 98
Shaun Jackman Avatar answered Oct 11 '22 18:10

Shaun Jackman


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") }) --- 
like image 40
betiko Avatar answered Oct 11 '22 19:10

betiko