Cross references between slides and its content - r

In an R markdown presentation with output format beamer (to generate a LaTex/PDF file), is it possible to create cross-references between slides, i.e. pages of the final PDF?
This would be very helpful to quickly jump between slides, e.g. to navigate to an appendix at the end of the presentation.
I tried to use bookdown commands as proposed in this SO post, but without success.
MWE:
---
title: "Cross references between slides"
output:
# beamer_presentation: default
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Bullets with references
- Bullet 1: \ref{tab:my-table}
- Bullet 2: \ref{fig:my-plot}
- Bullet 3: \ref{appendix}
## Bullets with references (bookdown)
- Bullet 1: \#ref(tab:my-table)
- Bullet 2: \#ref(fig:my-plot)
- Bullet 3: \#ref(appendix)
## table
```{r my-table, cars, echo = TRUE}
library(kableExtra)
kable(summary(cars))
```
## plot
```{r my-plot, pressure}
plot(pressure)
```
## appendix
my appendix

For linking to the appendix, you can use
- Bullet 3: \hyperlinkappendixstart{appendix}
If you examine the tex code produced by your MWE you will see that your table and figure are both included without caption or figure/table environment, but you can reference the slide they are on
- Bullet 1: \hyperlink{table}{table}
- Bullet 2: \hyperlink{plot}{plot}
MWE:
---
title: "Cross references between slides"
output:
beamer_presentation:
theme: "default"
keep_tex: true
includes:
in_header: preamble.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Bullets with references
- Bullet 1: \hyperlink{table}{table}
- Bullet 2: \hyperlink{plot}{plot}
- Bullet 3: \hyperlinkappendixstart{appendix}
## table
```{r my-table, cars, echo = TRUE}
library(kableExtra)
kable(summary(cars))
```
## plot
```{r my-plot, pressure}
plot(pressure)
```
## appendix
\appendix
my appendix
Approach 2
or you could use the caption package to add captions to your tables and plots
---
title: "Cross references between slides"
output:
beamer_presentation:
theme: "default"
keep_tex: true
includes:
in_header: preamble.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Bullets with references
- Bullet 1: \ref{foo}
- Bullet 2: \ref{bar}
- Bullet 3: \hyperlinkappendixstart{appendix}
## table
```{r my-table, cars, echo = TRUE}
library(kableExtra)
kable(summary(cars))
```
\captionof{table}{foo}
\label{foo}
## plot
```{r my-plot, pressure}
plot(pressure)
```
\captionof{figure}{bar}
\label{bar}
## appendix
\appendix
my appendix
using this as preamble.tex:
\setbeamertemplate{caption}[numbered]
\usepackage{caption}

Related

RMarkdown Beamer make slide titles and add figures in loop

I am new to RMarkdown and Beamer. I am trying to make a set of slides where each slide contains a JPG. I have a dozen to loop through. How do I set this up in a loop?
Here is the RMD file:
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
---
# Introduction
## Blah
- Text
- Here
- Etc.
# Images
## pic_1
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_1.jpg")
```
## pic_2
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_2.jpg")
```
## pic_3
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_3.jpg")
```
## pic_4
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_4.jpg")
```
I know I can put the slide titles and figure paths into a data frame, but I am not sure how to do this inside RMarkdown and how to loop through it to build the slide titles and insert the images.
title <- c('pic_1', 'pic_2', 'pic_3', 'pic_4')
fpath <- c('modelA_pic_1.jpg', 'modelA_pic_2.jpg', 'modelA_pic_3.jpg', 'modelA_pic_4.jpg')
fpath <- paste0("../images/", fpath)
myfiles <- data.frame(title, fpath)
Updated Based on Accepted Answer
Below is what I ended up using for my Rmd. This page explains the basics of the xmpmulti package.
For this set up, my RMD is in one folder; the images are one folder up (../) and then in a folder called temp (../temp/). The images in this folder are named test-1.png, test-2.png, etc.
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
header-includes:
- \usepackage{xmpmulti}
---
# Introduction
## Blah
- Text
- Here
- Etc.
```{=latex}
\end{frame}
\section{Images}
\begin{frame}
\frametitle<1>{picture 1}
\frametitle<2>{picture 2}
\centering
\multiinclude[format=png,start=1,end=2,graphics={width=1\textwidth}]{../temp/test}
\end{frame}
\begin{frame}
```
some test
Assuming your images are named pic-1.png etc., then beamer has an automatic way to loop over the images via the xmpmulti package:
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
keep_tex: true
header-includes:
- \usepackage{xmpmulti}
---
# Introduction
## Blah
- Text
- Here
- Etc.
```{=latex}
\end{frame}
\section{Images}
\begin{frame}
\frametitle<1>{picture 1}
\frametitle<2>{picture 2}
\frametitle<3>{picture 3}
\centering
\multiinclude[format=png,start=1,end=3,graphics={width=.6\textwidth}]{pic}
\end{frame}
\begin{frame}
```
some test

About the hyperlink in RMARKDOWN

In rmarkdown, we can create a catalog using 'toc: yes',using the hyperlink, i can link to subtitle.
Now, i want to link subtitle to catalog ? (in the final html, i with to click subtitle,then back to top of the file)
---
title:'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```
You may enclose subheader in an HTML tag <a>.
---
title: 'my markdown file'
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## subtitle 1 :R Markdown
```{r cars}
summary(cars)
as.matrix(rnorm(100))
```
## subtitle 2:Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```

Multiple chunks in a tab

I want chunks of Example1 and Example2 in a single tab and Example3 in another tab. The code below is showing everything in one tab. In my desired output, some chunks in sectionA tab and some in sectionB tab.
---
title: "R Notebook"
output:
html_document:
df_print: paged
code_folding: show
theme: united
highlight: tango
toc: true
toc_float: true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
---
```{r, echo=FALSE}
library(rmarkdown) #used for syntax highlighting in this document
```
## Sections {.tabset}
### Section A
# Example1
```{r, eval=FALSE}
plot(cars)
```
<details>
<summary>Click for Output</summary>
```{r, echo=FALSE, eval=TRUE}
plot(cars)
```
</details>
### Section B
# Example2
```{r}
quantile(mtcars$mpg, probs = c(0.99))
```
## {-}
# Example3
```{r table}
knitr::kable(mtcars[1:5,, 1:5], caption = "A table caption")
``````
If I understand correctly, you want something along these lines? Once you start a tabbed section, you can't willy-nilly throw around subsections but should follow specific hierarchy. If you have a tabbed section of level ##, everything under level ### will become a tab. Anything deeper than that will become a title in the said tab.
## Sections {.tabset}
### Section A
#### Example1
```{r, eval=FALSE}
plot(cars)
```
<details>
<summary>Click for Output</summary>
```{r, echo=FALSE, eval=TRUE}
plot(cars)
```
</details>
#### Example2
```{r}
quantile(mtcars$mpg, probs = c(0.99))
```
### Section B
#### Example3
```{r table}
knitr::kable(mtcars[1:5,, 1:5], caption = "A table caption")
``````

How to Create Full Width Figures When Using the "twocolumn" "classoption" for Knitr to PDF?

How can one create a full width figure when using the twocolumn class option in knitr / R / RMarkdown / LaTex?
Based on the Knitr documentation, I've tried two approaches. Nothing short of editing the .tex file has worked for me so far.
This:
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-2]
```{r fig.env = "figure*", fig.cap = "Test"}
plot(runif(10))
```
\lipsum[3-5]
```{r fig.fullwidth = T}
plot(runif(10))
```
Results in this:
Yihui has fixed this in the development version of knitr. Yihui's response:
The option fig.env = 'figure*' should be respected now (in the dev version of knitr). But the plot will float to a new page. I guess that is a LaTeX issue orthogonal to knitr. Thanks!
Don't forget that you must include a caption for this to work.
---
output: pdf_document
classoption: twocolumn
header-includes:
- \usepackage{lipsum}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[1-3]
```{r fig.env = "figure*"}
plot(runif(10))
```
\lipsum[2]
```{r fig.env = "figure*", fig.cap = ""}
plot(runif(10))
```
\lipsum[2]

hiding entire pages in an r markdown document

I have the r markdown document below. I would like to hide page 2 if parameter "P" is not equal to A.
So the result would be if parameter P != A then only 3 pages are produced.
Is this possible somehow?
---
title: "Untitled"
output:
pdf_document:
toc: yes
params:
P: A
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
## PAGE3
this is text for page 3
```{r pressure, echo=FALSE}
plot(pressure)
```
\newpage
## PAGE4
this is text for page 4
You could comment the section out based on P value. Here is a bit of a hackish way to do it: surround the section with \iffalse/\fi. Note that the R code inside the section still needs to be valid for this to work (it will be compiled by knitr, but ignored by latex).
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\iffalse")
```
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\fi")
```

Resources