Setting document title in Rmarkdown from parameters - r

I've got an Rmarkdown template that works well, and I've parameterized it so I can generate variants of the same report from different data sources. However, I'd like to change the title of the report in each case. How do I do that?
Here's the YAML header I have so far:
---
title: "My Title"
author: "Me, Inc."
date: "August 4, 2015"
output: pdf_document
params:
title: default
---
I've tried using params=list(title="ASDF") in the call to rmarkdown::render, and although my code can see that variable, it doesn't change the title. I've also tried using r params$title in the YAML, but that gives a syntax error.
Is there something else I should be trying? Thanks!

Try to use a second YAML metadata block, and put the parameterized metadata in there.
I got the following code to work as expected (i.e., producing a document title from the list of params):
---
output: html_document
params:
set_title: "My Title!"
---
---
title: `r params$set_title`
---
The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.
Update (2017):
This can be accomplished in a single block, like so:
---
output: html_document
params:
set_title: "My Title!"
title: "`r params$set_title`"
---
This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".

This is a more simplified approach to the dynamic title challenge.
Decouple title from the top declaration like this:
From this:
---
title: "Sample Title"
output: pdf_document
---
To This:
---
output: pdf_document
---
```{r}
title_var <- "Sample Title"
```
---
title: `r title_var`
---
Within the R code chunks, declare title_var. Now the title is held within a variable. Hope this helps!

Adding this answer as it helps in making R markdown titles dynamic.
Just use !r followed by the object name defined (test_title in the case below) to make the title dynamic.
---
output: pdf_document
params:
set_title: !r test_title
---
---
title: `r params$set_title`
---

Related

R error message output goes off the slide in beamer presentation R markdown

I'm using R markdown to make a beamer presentation.
When I try to include an error message output from R on the slide it goes off the page.
Here is my code:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output: beamer_presentation
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```
And, this is what the slides look like:
How do I keep the code on the page, either by putting it on multiple lines or reducing font size?
You can use the same technique as in Change representation (background-color & frame) of chunks in RMarkdown (with Beamer-Presentations) to redefine the verbatim environment in such a way that it uses the listings package. The listings package then allows you to both add line breaks and adjust the font size to your liking:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output:
beamer_presentation:
keep_tex: true
header-includes:
- \let\verbatim\undefined
- \let\verbatimend\undefined
- \usepackage{listings}
- \lstnewenvironment{verbatim}{\lstset{breaklines=true,basicstyle=\ttfamily\footnotesize}}{}
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```

Cross reference and caption not working in Rmd file

Can anyone help me understand how to write my header so that the figure caption and cross reference works?
I am practicing making captions and cross references to a simple plot in my Rmd file. I understand that to do so, I should add to my header: "output: bookend::pdf_document2" and "fig_caption = yes". Then, to a chunk called myfigure, I should add "fig.cap = "\label{fig:myfigure} My caption". To cross reference this figure I should write in the text "#ref(fig:myfigure)". My code is below. It won't knit because the formatting of the header is wrong.
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```
My plot is called \#ref(fig:myfigure).
Then, I tried deleting the whitespace before toc and fig_caption, and it knit, but no caption appeared, and the text literally printed "#ref(fig:myfigure)" instead of a cross reference. The header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
I also tried adding "pdf_document:" to the header, but the same issue of no caption and the cross reference being literally "#ref(fig:myfigure)". This header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
pdf_document:
toc: true
fig_caption: yes
---
Can anyone help me understand how to write my header so that it works?
use \ref{fig:myfigure} instead of \\#ref(fig:myfigure)
See RStudio Community Post
You have a wrong YAML header and some wrong understanding of referencing. I used this RMD file:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output:
bookdown::pdf_document2:
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "My caption"}
plot(pressure)
```
My plot is called Figure \#ref(fig:myfigure).
First, break the line after output in the header. The whitespaces are very important in the YAML header!
Then, read the bookdown manual:
The label of a figure environment is generated from the label of the code chunk, e.g., if the chunk label is foo, the figure label will be fig:foo (the prefix fig: is added before foo). To reference a figure, use the syntax, where label is the figure label, e.g., fig:foo.
To reference your plot with the chunk name "myfigure", just write \#ref(fig:myfigure). The caption of the figure can be set via fig.cap in the chunk options.

Evaluating R code in YAML header

Consider the following Rmd file,
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output: html_document
test: "`r paste('_metadata.yaml')`"
---
```{r}
cat(rmarkdown::metadata$test)
```
The date is processed (knitted) by R before being passed to pandoc for conversion to md and html. The custom field test, however, is unevaluated.
What's the difference? Can one force knitr/rmarkdown to evaluate an arbitrary field in the yaml header?
Note: the actual purpose is not to just print() a filename as in this dummy example, but to load an external yaml file containing metadata (author information), process it with R, and output a string that will be injected in the document.
It does evaluate the code. If you run foo.Rmd with
rmarkdown::render("foo.Rmd", clean = FALSE)
you'll see an intermediate file (the pandoc input) called foo.knit.md left behind. It will look like this:
---
title: "Untitled"
author: "baptiste"
date: "2017-08-12"
output: html_document
test: "_metadata.yaml"
---
```r
cat(rmarkdown::metadata$test)
```
```
## `r paste('_metadata.yaml')`
```
I don't know how to see that from within the document (your example shows that metadata$test doesn't work), but there's probably some trick or other to get at it.
The standard metadata field data and your custom field test are not actually treated any differently. This code:
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
test: "`r paste('_metadata.yaml')`"
---
```{r}
cat(rmarkdown::metadata$date)
cat(rmarkdown::metadata$test)
```
leads to the following output:
As you can see, also date was not evaluated. I have not found any functionality in the rmarkdown or knitr packages. But the following simple function does the trick at least for your simple example:
---
title: "Untitled"
author: "baptiste"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
test: "`r paste('_metadata.yaml')`"
---
```{r}
eval_meta <- function(x) eval(parse(text = gsub("`|r", "", x)))
eval_meta(rmarkdown::metadata$date)
eval_meta(rmarkdown::metadata$test)
```
Whether that works in your more complex situation is another question, however.

How to remove compact title from R markdown to latex conversion?

I wrote my own titlepage and it is loaded via an include in the R-markdown file. However, this conflicts with the pandoc title. I am trying to find settings in the R markdown yaml header such that pandoc does not insert the following code snipped into the tex-file.
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{}
\pretitle{\vspace{\droptitle}}
\posttitle{}
\author{}
\preauthor{}\postauthor{}
\date{}
\predate{}\postdate{}
There is no clear indication in the pandoc documents or the r markdown guidelines how to disable the title generation. Any help would be appreciated.
Update: In particular, I am looking for solutions that allow me to keep creating my title page with the \maketitle command. That is why I focussed on this particular code snipped that I want to get rid of.
I also use my own title page with rmarkdown documents for latex/pdf outputs. To remove the title, you can add the following command in a text file called with in_header :
\AtBeginDocument{\let\maketitle\relax}
A reproductible example with the header.tex file built directly within the Rmd document:
---
title: "RMarkdown No title Test"
author: "StatnMap"
date: "July 30, 2017"
output:
pdf_document:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r rm_title_page, echo=FALSE}
head <- cat('
\\AtBeginDocument{\\let\\maketitle\\relax}
', file = "header.tex")
```
# Title 1
**Some text**
# Title 2
**Some text**
Using compact-title: false in the YAML works.
---
title: "This title is not compact"
author: "Test"
date: "2019 May 10"
output: pdf_document
compact-title: false
---
I had the same problem today. Here's what I did. (Maybe I'll update the solution when I come up with something better.)
The solution is dumb but useful. I can't set an arbitrary space between the lines now, because I used \newline.
---
title: "\\huge My Smart Title"
author: "\\newline \\Large My Smart Author"
date: "\\newline \\Large 2018-12-25"
output:
pdf_document:
includes:
in_header: preamble.tex
latex_engine: xelatex
---
Below are the outputs before and after the solution.
BEFORE:
AFTER:
Note:
You may be confused about the different sizes of the "author" and the "date" in the two pictures above, if you don't know that the fontsize of the "author" and the "date" is \large instead of \Large by default.
END

Split the title onto multiple lines?

In an R markdown document (html and presentations), is it possible to manually split the title onto multiple lines? I tried playing with pipes which produces orrendous output.
---
title: 'A title I want to split on two lines'
author:
date:
output:
ioslides_presentation
---
Examples for adding an abtract show the use of pipes | to break lines and include paragraphs. This works as well for the title and other yaml elements. For an abstract or title:
---
abstract: |
What works for the abstract.
Works for the title, too!
title: |
| title
| subtitle
output: pdf_document
---
For an HTML output just us the <br> tag while if your output is a PDF or PDF presentation standard LaTeX code to break line given by \\ should work.
Example
---
title: 'A title I want to <br> split on two lines'
author:
date:
output:
ioslides_presentation
---
For PDF
Just to rule out possibilities, I've tried to put \\ or \newline, both do not split, so for PDF seems to be a little bit tricky. \linebreak stop knitr parsing. Maybe another user can solve this question for knitr PDFs.
For PDF output, experimentation revealed that the following works:
---
title: 'A title I want to \nsplit on two lines'
author:
date:
output: pdf_document
---
That is two spaces followed by \n.

Resources