How do you embed R markdown code within R Markdown? - r

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

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

Specify custom powerpoint theme in R markdown

I am using R markdown to create a presentation. Here's a sample code:
---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: powerpoint_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with Plot
```{r pressure, fig.width=30, fig.asp=0.618,
out.width="200%"}
plot(pressure)
```
I want to apply a custom template that is specified by my organisation. How do I specify the custom theme? I read online that this can be specified by:
---
title: "Untitled"
author: "April 2018"
date: "4/9/2019"
output: powerpoint_presentation:
reference_doc: my_theme.pptx
---
But how does this work? Where do I have to store the my_theme.pptx file?
I hope you don't need this answer now, but just in reference for all of the guyz who were seeking for the answer like me.
Go to >> R\win-library\version e.g. 4.0\officer >>> paste your custom template on this path.
It will work

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

How to change table of content header in knitr?

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.

Resources