I want to add the table of content in different place instead of the first page in .Rmd pdf. I found the function render_toc() from #Garrick Aden-Buie this is the link https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8. It is working perfect if I don't use cat() in chunk codes.
I have to use cat() in my .Rmd, is there any way I can add the table of contents any place in .Rmd
You can see the table of contents only test 1, test 2, and test 3, setosa, versicolor, virginica did not include in table of content
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: yes
toc_depth: 4
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("render_toc.R")
library(knitr)
```
\newpage
# Table of Contents {#crazy-slug-here}
```{r echo = FALSE}
render_toc("test.Rmd")
```
# Test 1
# Test 2
# Test 3
```{r echo = FALSE, results ='asis'}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,], aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat('\n')
}
```
Using the code you provided I'm able to knit the pdf document and get the three plots, one for each Species in iris.
This code, however, produces two TOCs: one of them in the first page which is not desired according to your question. In order to get only the TOC produced by the function render_toc() set toc: no in the YAML header.
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: 4
---
This way only one TOC will be generated and placed where render_toc() is.
Edit: After seeing your edit with the desired output, I think the following LaTeX code can help:
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
Note: here we are not using render_toc(). The code creates the TOC in the second page of the document. If you need another blank page, just introduce another block of:
\newpage
\thispagestyle{plain}
\mbox{}
These pages may or may not be numbered, which you control via the argument passed to \thispagestyle{}. See here for more details.
Full code:
---
title: "Document title"
output:
pdf_document:
number_sections: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
# Test 1
## Subsection 1
# Test 2
## Subsection 2
# Test 3
```{r, results='asis', echo=FALSE}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,],
aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat("\n")
}
```
This way we end up with a TOC looking like this:
Related
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
When using bookdown (single document), if I set both section_numbering = 'yes' and fig_caption = 'yes', the figures are numbered X.2 (where X is the section number). If section_number = 'no', the figures are numbered sequentially (Fig 1, 2 ...), but sections numbers are lost.
Is there a way to get figures numbered sequentially without losing the section numbers? In the example below, I would like to have both the sections figures numbered as 1 and 2.
Thank you.
---
output:
bookdown::html_document2:
fig_caption: yes
number_sections: yes
---
# header 1
Reference example: \#ref(fig:plotcars):
```{r plotcars, fig.cap = "A car plot"}
plot(cars)
```
# header 2
Reference example: \#ref(fig:plotcars2):
```{r plotcars2, fig.cap = "A car plot"}
plot(cars)
```
I just added a new argument global_numbering to the dev version of bookdown. You can test the dev version via
remotes::install_github('rstudio/bookdown')
Example:
---
output:
bookdown::html_document2:
fig_caption: true
number_sections: true
global_numbering: true
---
# header 1
Reference example: \#ref(fig:plotcars):
```{r plotcars, fig.cap = "A car plot"}
plot(cars)
```
# header 2
Reference example: \#ref(fig:plotcars2):
```{r plotcars2, fig.cap = "A car plot"}
plot(cars)
```
Im new to Rmarkdown and I would like to create dynamic reports where every report section is generated from a template (child) document. Each section will then start with a newpage in the rendered pdf.
My approach is currently based on this post which shows how to generate dynamically text in the child (which works), however I am not able to transfer the contents of the loop into a R-Codeblock, probably because the params are not well defined in the way that I tried to do it.
This is how my parent document looks like:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blabla
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
params <- list(species = i)
}
```
`r paste(knit(text = out), collapse = '\n')`
and this is how the child looks like
---
title: "template"
output: html_document
params:
species: NA
---
# This is the reporting section of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste(params$species)
# plot doesnt work work
# plot(iris$Sepal.Length[iris$Species=={{i}}],
# iris$Sepal.Width[iris$Species=={{i}}]
# )
```
\newpage
To my understanding the parameter that is actually passed is the last species from the dataset generated in the loop but I'm not sure why the plot would't work then. Can anybody help me out on how to fix this issue?
Ok. No need to go through params. The solution was simply to put i between brackets AND parenthesis in the child-document.
Parent:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blahblah Main text before individual sections
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
}
```
`r paste(knit(text = out), collapse = '\n')`
Child
---
title: "template"
output: html_document
---
# This is the reporting page of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste("This will be a plot of Sepal Length and Witdh from", '{{i}}')
plot(iris$Sepal.Length[iris$Species=='{{i}}'],
iris$Sepal.Width[iris$Species=='{{i}}']
)
```
\newpage
Original solution found here.
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")
``````
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")
```