rmarkdown - export table of contents - r

I have an RMarkdown file used for creating a QC report on other sets of data, and the report is relatively long. My table of contents essentially contains the name of all the checks I run on the data, and I'd like to have the table of contents itself exported into a new document, for outside users to quickly see what checks are run.
Is there any way to export just the TOC to word/pdf/html, or otherwise 'roll up' and export the different sections of an RMarkdown file?

I'm not entirely clear on what you're trying to achieve.
Perhaps you're after a "floating" TOC, like this?
---
title: "Title"
output:
html_document:
toc: true
toc_float: true
---
# Section 1
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
# Section 2
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```

For a html_document output format, you can set the body Pandoc variable to a void string:
---
title: "Title"
output:
html_document:
toc: true
pandoc_args: ['-V', 'body=""']
---
This solution will not work for a pdf_document or a word_document.

Related

How to not run a chunk that will have an error

I am working to knit an html file with images that will be updated weekly. I am using an Rmarkdown file to specify file path to images as so:
---
title: "test"
output:
html_document:
toc: true
toc_depth: 2
toc_float: true
theme: flatly
params:
user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---
I will be generating these report weekly so one week we might have an image for user 2 and one week we might not have an image for user 2. So I know that I can knit and get an html running with setting error = TRUE. But that still displays the error message in the html output where the image is missing. Is there a way to set up a condition where if a file is not present, that chunk will not run where I read in the file? Im using magick package to read in each image from it path specified in the params.
you can pass R statements to knitr options as well, as in the following.
---
title: "test conditional chunks"
output: html_document
params:
user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r condition 1, echo=FALSE, eval=file.exists(params$user1)}
# plot image
```
```{r condition 1, echo=FALSE, eval=file.exists(params$user2)}
# plot image
```

Cross reference and caption not working in Rmd file

Can anyone help me understand how to write my header so that the figure caption and cross reference works?
I am practicing making captions and cross references to a simple plot in my Rmd file. I understand that to do so, I should add to my header: "output: bookend::pdf_document2" and "fig_caption = yes". Then, to a chunk called myfigure, I should add "fig.cap = "\label{fig:myfigure} My caption". To cross reference this figure I should write in the text "#ref(fig:myfigure)". My code is below. It won't knit because the formatting of the header is wrong.
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```
My plot is called \#ref(fig:myfigure).
Then, I tried deleting the whitespace before toc and fig_caption, and it knit, but no caption appeared, and the text literally printed "#ref(fig:myfigure)" instead of a cross reference. The header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
I also tried adding "pdf_document:" to the header, but the same issue of no caption and the cross reference being literally "#ref(fig:myfigure)". This header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
pdf_document:
toc: true
fig_caption: yes
---
Can anyone help me understand how to write my header so that it works?
use \ref{fig:myfigure} instead of \\#ref(fig:myfigure)
See RStudio Community Post
You have a wrong YAML header and some wrong understanding of referencing. I used this RMD file:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output:
bookdown::pdf_document2:
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "My caption"}
plot(pressure)
```
My plot is called Figure \#ref(fig:myfigure).
First, break the line after output in the header. The whitespaces are very important in the YAML header!
Then, read the bookdown manual:
The label of a figure environment is generated from the label of the code chunk, e.g., if the chunk label is foo, the figure label will be fig:foo (the prefix fig: is added before foo). To reference a figure, use the syntax, where label is the figure label, e.g., fig:foo.
To reference your plot with the chunk name "myfigure", just write \#ref(fig:myfigure). The caption of the figure can be set via fig.cap in the chunk options.

Number some sections with rmarkdown

I am writing a manuscript with rmarkdown.
If I want to number all sections, I can use YAML like this
---
title: "My Report"
output:
html_document:
number_sections: true
---
See Automatically number sections in RMarkdown
But I only want to number some sections, so my document looks like
Abstract
1. Introduction
2. Methods
3. Results
References
Is there anyway to do this?
From the Pandoc User Guide you want to add the .unnumbered attribute to the header. This is done using:
# My header {.unnumbered}
or the shortcut
# My header {-}
For example, using the following document
---
title: "My Report"
output:
html_document:
number_sections: true
---
# Abstract {-}
# Introduction
# Methods
# Results
# References {-}
The HTML produced renders as:

How to produce both .html and .md from an .Rmd (Rmarkdown) and rename them?

I know making .Rmd produce .html file or .md file should use the following codes
---
title: "report"
output: html_document
---
or
---
title: "report"
output: md_document
---
But how to produce the two at the same time? I try the following, but it is not working
---
title: "report"
output: html_document, md_document
---
Another issue is that, how can I make the name of the .html file (or .md file) produced different from the .Rmd file? For example, I have sample.Rmd, but I want the .md file to be named as sample_1.md.
You can use keep_md: yes to keep the md file while still getting other output. So the YAML will be something like
---
title: "report"
author: "me"
date: '2016-04-25'
output:
html_document:
keep_md: yes
---
You can customise the knit button, so that you can simultaneously render one Rmd file to multiple outputs.
Press knit button once, and then get two outputs (.html/.md) from the example below.
```
knit: (function(input, ...) {
rmarkdown::render(
input,
output_format = "all"
)
title: "report"
output:
html_doclument:
number_sections: true
toc: true
md_document:
variant: "markdown"
number_sections: false
toc: false
```

How to add table of contents in Rmarkdown?

I am using RStudio for writing markdown documents and want to add Table of Contents (TOC) at top of the documents so that the user could click the relevant section for reading. There were some relevant examples on rpubs but now I can't seem to find them. Please note that I don't use pandoc and am quite new to Rmd & knitr. Is there any way to add TOCs without using pandoc? If using pandoc is must then which functions are relevant?
EDIT
Here's a small sample page:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
Header 1
---------------
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>.
## Header 2
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}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
### Header 3
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I tried running this in RStudio v 0.98.864 and it worked! but sadly it didn't work on 0.98.501 and 0.98.507. I am working on my thesis in 0.98.501 and after updating RStudio, some of my analyses didn't work. So, I reverted back to 0.98.501.
What should I do now? I really want TOCs but without harming the outputs of other analyses.
The syntax is
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.
Syntax with more options:
---
title: "Planets"
author: "Manoj Kumar"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true # table of content true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
number_sections: true ## if you want number sections at each table header
theme: united # many options for theme, this one is my favorite.
highlight: tango # specifies the syntax highlighting style
css: my.css # you can add your custom css, should be in same folder
---
If you are using pdf_document, you might want to add table of contents in a new page, which toc: true does not allow. It puts the table of contents right after the document title, author and date--because it is in yaml.
If you want to have it in a new page, you have to use some latex language. Here is what I did.
---
title: \vspace{3.5in}"Title"
author: "Name"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: true
number_sections: true
---
\newpage # adds new page after title
\tableofcontents # adds table of contents
\listoffigures
\listoftables
\newpage
So, after yaml (the chunk between ---), I added a new page using \newpage, then a table of contents using \tableofcontents, a list of figures using \listoffigures, a list of tables \listoftables, and a new page before everything else.
Note, \vspace{3in} in the title adds vertical space of 3 inch from the top before printing yaml (title, etc.).
Read more here: https://www.sharelatex.com/learn/Table_of_contents

Resources