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.
Related
I am teaching students how to create/edit R markdown files. so I would like to have an R markdown template within my R markdown file. An example is below:
---
title: "file_check"
author: "James"
date: "8/7/2020"
output: html_document
---
Learn by copying this into blank .Rmd file:
---
title: "Template"
author: "Your Name"
date: "Specify Date"
output: html_document
---
This is how you plot
```{r}
plot(1:10)
```
When I run this I obviously get errors but I know there has to be a fast/efficient way to do this since the R markdown website has this capability.
You could use a text chunck :
---
title: "file_check"
author: "James"
date: "8/7/2020"
output: html_document
---
Learn by copying this into blank .Rmd file:
```{text}
---
title: "Template"
author: "Your Name"
date: "Specify Date"
output: html_document
---
```
This is how you plot
```{r}
plot(1:10)
```
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
The following header is from a R Markdown document which I have compiled as a PDF. It results in code snippets being cropped. Commenting out the PDF output block and uncommenting the HTML block results in well-formatted HTML output.
Is there a parameter I can change to fix this? Or do I need to format my code snippets differently?
---
title: "fmodbc Package"
author: "Bobby Rohrkemper, Software Developer at Schweiz Tourismus"
date: "`r Sys.Date()`"
# output:
# rmarkdown::html_vignette:
# toc: TRUE
output:
pdf_document:
toc: TRUE
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Or should I perhaps try a different theme? I would be interested in the Tufte Handout theme, but thought it makes more sense to fix the standard output first.
http://rmarkdown.rstudio.com/tufte_handout_format.html
PDF output is cropped:
HTML output looks good:
Code snippet:
This produces the results above. I am not modifying it in the PDF and HTML versions.
```{r}
# names(dat)
# "__Backups" "__Budget" "__Comments" "__Documents" "__globals" "__KPI" "__Marketing Activities" "__MarketManager" "__Segmentation" "__sts_Account" "__sts_Budget" "__sts_Mandate" "__sts_ProfitCenter" "__UserLog" "__VL_PlanningStatus"
```
Use the chunk option tidy = TRUE.
tidy: (FALSE; logical) whether R code should be tidied up using the function tidy_source() in the formatR package; if it failed to tidy up, original R code will not be changed; tidy=TRUE is like keep.source=FALSE in Sweave, but it also tries not to discard R comments (N.B. this option does not work in certain cases; see http://yihui.name/formatR for more information)
---
title: "fmodbc Package"
author: "Bobby Rohrkemper, Software Developer at Schweiz Tourismus"
date: "`r Sys.Date()`"
# output:
# rmarkdown::html_vignette:
# toc: TRUE
output:
pdf_document:
toc: TRUE
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r}
library(knitr)
opts_chunk$set(tidy = TRUE)
```
```{r}
# names(dat)
# "__Backups" "__Budget" "__Comments" "__Documents" "__globals" "__KPI" "__Marketing Activities" "__MarketManager" "__Segmentation" "__sts_Account" "__sts_Budget" "__sts_Mandate" "__sts_ProfitCenter" "__UserLog" "__VL_PlanningStatus"
```
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`
---
I am using the following options to produce a pdf document with knitr:
---
title: "Test"
author: "Paulo Miramor"
date: "13-07-2015"
output: pdf_document
toc: yes
---
I would like to change the header of the table of contents (which is "Contents"), since I am producing a document in Portuguese. Is there any way to customize it?
Thanks to #Molx and #Chris in the comments I could find a solution.
Solution 1
Add \renewcommand{\contentsname}{Índice} to the document so that the .Rmd header is:
---
title: "Test"
author: "Paulo Miramor"
date: "13-07-2015"
output: pdf_document
header-includes:
- \renewcommand{\contentsname}{Whatever}
toc: yes
---
With this solution the header is Whatever you put inside \contentsname argument.
Solution 2
Add lang: portuguese to the document so that the .Rmd header is:
---
title: "Test"
author: "Paulo Miramor"
date: "13-07-2015"
output: pdf_document
lang: portuguese
toc: yes
---
Using this solution the header was a translation of "Contents" to Portuguese. This should work if your TeX installation supports the language.