Specifying multiple simultaneous output formats in knitr - r

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
---

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
---

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") })
---

Related

Change default YAML of Rmarkdown file

I want to change the default template of the Rmarkdown document. I edited the file named document.Rmd in the templates in the RStudio application folder so that the YAML is:
---
title: 'document'
output:
html_document:
self_contained: true
toc: true
toc_float: true
bookdown::pdf_document2:
extra_dependencies: ["float"]
toc: true
---
However, when I open a new Rmarkdown document from RStudio, I get a prompt that asks what type of Rmarkdown document I want. I choose HTML, click OK, and then I get the file opened with these two YAMLs!
---
title: "Untitled"
output: html_document
---
---
title: 'document'
output:
html_document:
self_contained: true
toc: true
toc_float: true
bookdown::pdf_document2:
extra_dependencies: ["float"]
toc: true
---
I do not want to go through the hustle of creating a package or a template. This simple solution should work, I think.
My question: Does anybody know how to work around this? Thank you in advance!
After reading your question, I found this source, which indicated that you could add a template. I also read that document.RMD does not include the YAML. That's why you are getting a double-YAML.
Since I had never added a template for R Markdown, I went through the steps to see how it all came together. That led me to create a repository on Github, and well, here we are. Essentially, I made a Github repository that you can install with the exact template you wanted.
You can also look at the code, as well. Since it was created just to answer this question, if you wanted it to be different, you can change it, as well.
You'll find the repository here: fraupflaume/customYAML
If you want to install it:
devtools::install_github("fraupflaume/customYAML")
To use it, you don't have to call the library (there's no R code). Start an R Markdown script, select "From Template," then choose "Custom YAML."

How to change rmarkdown chunk background color when knitting to PDF

I am preparing a manuscript for publication and need a way to change (remove actually) the background color of code chunks in my PDF file (generated through Latex).
Reproducible example:
---
title: TestFile
output:
pdf_document: null
---
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
```
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
Another option, is to use Pandoc Syntax Highlighting styles, which is very simple and has very nice options. Compared to the LaTeX option you used, this is perhaps less flexible, but has the advantage of changing not only the background of code chunks, but also the font colour.
To do this, you only have to add the highlight style you want to the YAML. For example, adding the zenburn highlight:
output:
pdf_document:
highlight: zenburn
Will produce this:
Instead of this:
I hope this helps, but let me know if not.
I think I've found a solution. This is a purely latex solution, but it does the trick for what I need (which is outputting to PDF via latex) using the package xcolor. [PDF output] https://i.stack.imgur.com/yEADg.png
Reproducible Example:
---
title: TestFile
output:
pdf_document: null
header-includes: \usepackage{xcolor}
---
\definecolor{shadecolor}{RGB}{255,255,255}
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
```

R Markdown PDF Change Font & Color for Headers

I need to update the color and font of the header (only header) in a R Markdown PDF file. I have found recourses on how to do this for the whole document, but can't find an answer for changing the headers only.
Thank you kindly!
---
title: "Untitled"
output: pdf_document
---
Simplified version of the solution provided by Grada Gukovic:
You can add simple LaTeX statements to your document via the YAML header header-includes, e.g.:
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{sectsty}
- \allsectionsfont{\color{cyan}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for
authoring HTML, PDF, and MS Word documents. For more details on using
R Markdown see <http://rmarkdown.rstudio.com>.
Result:
This is most useful for small additions like the one seen here. If you want to add more than a few lines of LaTeX code it is often easier to save them to an external file, say preamble.tex and include that via
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
Other possible places are before_body and after_body, c.f. ?rmarkdown::includes.
There is no option to do this in rmarkdown::pdf_document. You can do this by modifying the .tex template being used using the sectsty package for latex.
For example the following changes the color of all headers to cyan:
Download the default latex template from here:
tex template
Open the template in Notepad and add the following lines on an appropriate place in the document preamble(I have them as lines nr. 200 and 201):
\usepackage{sectsty}
\allsectionsfont{\color{cyan}}
Save the modified file with the extension .tex (my file is called "Cyansections.tex") and put it in R's working directory.
Modify the header of the .rmd document:
---
title: "Untitled"
output:
pdf_document:
template: Cyansections.tex
---
If you want a different color or font consult this answer
and sectsty's manual Especially section 4 of the manual for chanhing fonts

In `_output.yml`, some parameters are getting ignored

I have defined certain pandoc options in _output.yml:
```
author: "abc"
date: "`r format(Sys.time(), '%d %B, %Y')`"
```
But, these get ignored, unless these are defined in the YAML header inside the R Markdown file.
Is this prohibited from including in the separate YAML file? Is so, could you please mention what other parameters should be defined inside the Markdown file.
An _output.yml file can only be used for setting the output formats, as explained within the bookdown book. So you can specify anything which relates to the output format (HTML/PDF/Word) such as:
html_document:
toc: TRUE
theme: flatly
pdf_document:
toc: FALSE
In your examples, the parameters you provide are about document content. So anything like author, title, date, fontsize can't be specified.
This issue has also been addressed within the GitHub issues of R Markdown for further reading.

Header style in R markdown

I am writing a report in R markdown (using pdflatex to convert it into pdf) and I am trying to set the style of the different headers (color, size, interline before and after).
I am using this formalisation :
#H1
##H2
###H3
I have the basics of Markdown and LaTeX but I'm far to be an expert. Is there a way to do it without creating a LaTeX template ? For exemple with some YAML code in the head of the document ?
Mine for now :
---
title: "Guide"
output:
pdf_document:
fig_caption: yes
highlight: tango
toc: yes
toc_depth: 2
documentclass: report
---
Or by changing the R options ?
```{r set-options, echo=FALSE, cache=FALSE}
options(some stuff)
```
I've found some indications while searching that suggest it may be possible, but I could'nt manage to find any understandable exemple ...
Thanks for any feedback.
If you do not want to do any LaTeX, you could add your own CSS (see documentation) and set it that way, as in this answer.
You would then need to do: How to convert Markdown + CSS -> PDF?
However, your results may not be as faithfully rendered as if you create a LaTeX template.

Resources