Use YAML variables within rmarkdown document - r-markdown

Say, I have an Rmd file that looks like this.
---
title: This is a title
subtitle: This is a subtitle
---
How can I access this variable title in the Rmd file like below?
```{r}
print(title)
```
or
`r print(title)`

You can access YAML metadata with rmarkdown::metadata.
---
title: "This is a title"
output:
html_document
---
The title of this document is **`r rmarkdown::metadata$title`**

Related

Render multiple outputs from one Rmd

This is allegedly possible, but when I use the code example from the RMarkdown Book, I get only one output, whichever output is listed first.
---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
---
````{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = function() {
if (knitr::is_latex_output()) 'latex' else 'pandoc'
})
````
I am using the most up-to-date versions of RStudio 2022.07.1 & knitr=1.39
Option 1
you can use rmarkdown::render in knit yaml key and specify ouput_format = "all" to render the documents to all the specified formats in the yaml section.
---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "output", output_format = "all") })
---
```{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>.
After clicking on Knit, you will have both pdf and html file in the output folder.
Option 2
Which is actually the same thing as option 1, but uses a bit different workflow.
In your Rmd file yaml section, you simply specify the output format like this,
---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
---
```{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>.
Then in the console or another r-script, run this,
rmarkdown::render('multiple_output.Rmd',output_dir = "output", output_format = 'all')
Again, similarly you will have both pdf and html file in the output folder.

How to translate words "Figure" and "Table" in bookdown::html_document2 documents?

In bookdown projects, it is possible to translate automatically generated words, such as "Table" or "Figure" by changing settings in the _bookdown.yml file (see https://bookdown.org/yihui/bookdown/internationalization.html). An how can I achieve the same result (translate the words) in bookdown::html_document2 documents?
The contens of my R Markdown document:
---
output:
bookdown::html_document2
---
```{r fig1, fig.cap=CAPTION}
CAPTION = "My plot. "
plot(women)
```
And the figure caption in the rendered document is:
I'd like to have "Fig." instead of "Figure".
You need to add a file _bookdown.yml in the same directory as your .Rmd. It should look like this:
_bookdown.yml
language:
label:
fig: "Fig. "
.Rmd
---
title: your doc
output: bookdown::html_document2
---
# Chapter 1
A reference to Figure \#ref(fig:women).
```{r women, echo=FALSE, fig.cap="CAPTION"}
plot(women)
```

Rendering issue with mermaid gantt diagram in Rmarkdown

I'm trying to insert a Gantt diagram in my Rmarkdown, using DiagrammeR mermaid.
The problem is when I knit to html, my text appears all greyish/transparent.
I noticed that when I remove the ## for headers, everything's fine.
Does anyone have a solution for me?
Here's the code template :
---
title: "My title"
author: "J. Doe"
output:
html_document:
toc: no
bibliography: biblio.bib
---
```{r libraries, include=FALSE}
library(DiagrammeR)```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)```
<style>
body {
text-align: justify}
</style>
## Table of content
1. [Project objectives](#bullet-1)
2. [Done and current tasks](#bullet-2)
3. [Bibliography](#bullet-3)
## 1. <a class="anchor" id="bullet-1"></a> Project objectives
PROJECT DESCRIPTION
## 2. <a class="anchor" id="bullet-2"></a> Done and *current* tasks
TASKS
```{r, echo=FALSE}
gantt=DiagrammeR::mermaid("
gantt
dateFormat YYYY-MM-DD
title A Very Nice Gantt Diagram
section Basic Tasks
This is completed :done, first_1, 2014-01-06, 2014-01-08
This is active :active, first_2, 2014-01-09, 3d
Do this later : first_3, after first_2, 5d
Do this after that : first_4, after first_3, 5d
section Important Things
Completed, critical task :crit, done, import_1, 2014-01-06,24h
Also done, also critical :crit, done, import_2, after import_1, 2d
Doing this important task now :crit, active, import_3, after import_2, 3d
Next critical task :crit, import_4, after import_3, 5d
section The Extras
First extras :active, extras_1, after import_4, 3d
Second helping : extras_2, after extras_1, 20h
More of the extras : extras_3, after extras_1, 48h
")
gantt```
## 3. BIBLIOGRAPHY
Many thanks!
J.

removing section in rmarkdown doc

I'd like to have the section titles in the header not the body of the page. \fancyhead[C]...puts the section title in the header but how do you suppress the body printing of the section?
---
title: "Untitled"
classoption: landscape
output:
pdf_document:
number_sections: false
dev: pdf
keep_tex: false
toc: yes
header-includes:
- \usepackage{fancyhdr}
- \usepackage{etoolbox}
- \pagestyle{fancy}
- \fancyhead[L]{left}
- \fancyhead[R]{right}
- \fancyhead[C]{ \nouppercase{\leftmark} }
---
```{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>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
\newpage
# Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\newpage
# page 3
You can also embed plots, for example:
```{r pressured, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

R Markdown - HTML Output in Powerpoint

I try to get Highouts Output in my Powerpoint presentation, but the suggestion does not work .This feature is still pretty new and I was not able to find something valueable in the docu.
---
title: "Untitled"
output:
beamer_presentation: default
powerpoint_presentation: default
ioslides_presentation: default
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(highcharter)
```
### R Markdown
This is an R Markdown presentation. 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>.
When you click the **Knit** button a document will be generated that
includes both content as well as the output of any embedded R code
chunks within the document.
### Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
### Slide with R Output
```{r cars, echo = TRUE}
highchart() %>%
hc_add_series(data = mtcars, type = "bar", hcaes(y = cyl))
```
### Slide with Plot
```{r pressure}
plot(pressure)
```
Produces:
Functions that produce HTML output found in document targeting pptx output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
I added the always_allow_html: yes part to the top, but it still does not work. Can anyone help me?