How to number table with chapter number - r

I inclue and cross-referencing a table in a Rmd file, and I want to add chapter number with the table title and referencing number, like "TABLE 1.1 test", but not "TABLE 1 test". I render the file using bookdown::word_document2. Here is a minimal example:
---
title: test
output:
bookdown::word_document2:
fig_caption: yes
toc: yes
---
```{r}
library(knitr)
```
# Heading 1
I'm cross-referencing a table table:\#ref(tab:test).
```{r test}
test <- data.frame(a = 1:6, b = 2:7)
kable(test, caption = "test")
```
Rmarkdown/Bookdown is really an amazing tool #Yihui, thanks!

Related

Link to supplementary table links to main text table in R Markdown

I have been using the following LaTeX command to create supplementary tables in my R Markdown documents:
\newcommand{\beginsupplement}{\setcounter{table}{0} \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
However, recently the linkage has broken. For example, when clicking on "Table S1" in-text, I will be taken to Table 1. Please see the example below:
---
title: 'Test Rmd document'
output:
pdf_document
header-includes:
- \newcommand{\beginsupplement}{\setcounter{table}{0} \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(tibble)
```
## Main body
This is a main text table (\autoref{main_table}). There is also a supplementary table (\autoref{supplementary_table}).
```{r}
table <- tibble(a = 1:3, b = 4:6)
kable(table,
caption = "Main body table \\label{main_table}",
format = "markdown")
```
\newpage
# Supplementary materials
\beginsupplement
```{r}
table <- tibble(a = 7:9, b = 10:12)
kable(table,
caption = "Supplementary table \\label{supplementary_table}",
format = "markdown")
```
Rendering the above gives this pdf document. Why is "(Table S1)" linking to Table 1?

bookdown figure number formattiing: both sequential numbering and section numbering

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

Pass Params from loop to generate Dynamic Reports in Rmarkdown

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.

R Markdown internal link to tables

Is there a possibility to create an internal link to a table in a rmarkdown-article?
I would like to click on this link and then I automatically come to my table.
I want to set the internal link in the text in the Empirical Results-part for table_1.
I've already tried [#table_1] but it doesn't work.
can someone help me?
Here is my example code:
---
title: "title"
subtitle: "subtitle"
author: "me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
keywords: "keywords"
output:
pdf_document:
fig_cap: yes
keep_tex: yes
documentclass: article
capsize: normalsize
fontsize: 11pt
geometry: margin=1in
spacing: doublespacing
footerdate: yes
abstract: 'Insert abstract here'
---
\newcommand*{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
\keywords{keywords}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
##required packages
library(tidyverse)
library(knitr)
library(kableExtra)
```
# Introduction
# Literature review
# Data and Methodology
# Empirical Results
In table_1 you can see something....
[#table_1]
# Conclusions
\newpage
# References
\newpage
# Appendix
```{r echo=F, warning=F, message=F}
df1 <- tibble(column1= 1:10, column2=11:20)
table_1 <-df1%>%kable("latex",
booktabs=T,
caption = "example table")%>%
kable_styling(latex_options = "hold_position")
table_1
```

How to use htmltools::tagList such that headers show in RMarkdown table of contents

I need to generate multiple htmlwidgets within a loop in an RMarkdown document. This is accomplished using htmltools::tagList. The following snippet will generate HTML output with 2 level 2 headings and a datatable in each section. The only problem is that I specified toc: true but though tags$h2(headers[i]) will render a level 2 headings on the page, those headings are not picked up by the table of contents. Is there a way get these headings in the TOC?
---
output:
html_document:
toc: true
---
## level 2 heading 0 - this one in TOC
```{r}
library(DT)
library(htmltools)
headers <- c("level 2 heading 1", "level 2 heading 2")
html_tags <- vector(mode = "list", length = 2*length(headers))
for (i in 1:length(headers)) {
html_tags[[(2*i)-1]] <- tags$h2(headers[i])
html_tags[[(2*i)]] <- list(list(datatable(iris)))
}
tagList(html_tags)
```
Note that in the result below the heading generated using "##" shows up in the table of contents, but the headings from the tagList call, though they render correctly in the document, are now in the table of contents.
Try this:
---
output:
html_document:
toc: true
---
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(DT)
library(htmltools)
```
```{r ,include = FALSE}
DT::datatable(iris)
```
## level 2 heading 0 - this one in TOC
```{r echo=FALSE, message=FALSE, warning=FALSE, results = 'asis'}
for (i in 1:2){
cat(" \n## level 2 heading", i, " \n")
print(htmltools::tagList(DT::datatable(iris)))
cat(" \n")
}
```
And the result is:
Maybe this is what you actually want.
Remember: You should avoid headers <- c("level 2 heading 1", "level 2 heading 2").
If you need hundred or thousand of headers, this isn't efficient and must waste too much time.

Resources