Restart Figure Numbering for Appendix / Supplementary Material in bookdown - r-markdown

I am writing an article for a journal that requires Supplementary Material. I was hoping to use a unique label such as SuppMat: instead of the default fig: to send Figures and Tables into this section. If not, I can use the default fig label but I need numbering to restart in the Supplementary Material section of the document.
I am aware of one answer using latex (found here) but I must complete this when exporting to docx. Below is a reproducible example using officedown
---
output:
officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(knitr, officedown, officer)
knitr::opts_chunk$set(echo = FALSE,
eval = TRUE,
fig.cap = TRUE)
```
# Main Text
Please see Supplementary Figure \#ref(fig:appendix-fig1) and Figure \#ref(fig:main-fig1).
```{r fig.id="main-fig1", fig.cap="This should be labelled **Figure 1**"}
barplot(1:5, col=1:5)
```
```{r tab.id="main-tab1", tab.cap="Main Text Table 1"}
head(mtcars)
```
\newpage
# Supplementary Materials {#SuppMat}
```{r fig.id="appendix-fig1", fig.cap="This figure should be labelled **Supplementary Figure 1**"}
barplot(1:5, col=1:5)
```
```{r tab.id="appendix-tab1", tab.cap="Should be labelled **Supplementary Table 1**"}
head(mtcars)
```

After searching multiple forums and hours, I was able to come up with a solution using the officedown package. Hopefully this helps someone else out. For further details check out the run_autonum function.
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(officedown, officer, knitr)
knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
ft_base <- fp_text(font.family = "Cambria", font.size = 12, bold = TRUE)
ft1 <- update(ft_base, shading.color='#EFEFEF', color = "red")
ft2 <- update(ft_base, color = "#C32900")
srcfile <- file.path( R.home("doc"), "html", "logo.jpg" )
extimg <- external_img(src = srcfile, height = 1.06/5, width = 1.39/5)
```
## References
This is a reference to an supplementary image caption whose number is Supplementary Figure \#ref(fig:faithfuld-plot) and \#ref(fig:supp-bar).
Testing another figure with a barplot in Figure \#ref(fig:plotbar)
```{r fig.id="plotbar", fig.cap = "Main Figure"}
barplot(1:8, col=1:2)
```
## Supplementary Material
```{r fig.id="faithfuld-plot"}
knitr::include_graphics("images/MyImage.png") # Use your own figure here (I needed to test with knitr for my workflow)
block_caption("First Appendix Figure",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'faithfuld-plot',
pre_label = "Supplemental Figure ",
start_at=1))
```
```{r fig.id="supp-bar"}
barplot(1:8, col=1:2)
block_caption("Second Appendix Figure",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'supp-bar',
pre_label = "Supplemental Figure ",
start_at= NULL))
```

Related

Rmarkdown - if condition is true/false print table

I am looking to conditionally display a table in a rmarkdown document. Some of the tables that are generated by my code may contain empty cells and I'm looking to exclude those tables and replace them with a message. Something like:
if_else(is.na(table[3,1]) == TRUE,"Data is currently not available",knitr::kable(table))
Is this possible?
we create a TRUE/FALSE variable in a previous R chunk, then depending on a boolean operation, we get a value we can pass to the eval= R chunk options. In this example due to the result, we get a graph, instead of a table. Remember when naming R chunks, each one has to have a unique name.
---
title: "Untitled"
output: html_document
---
```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
if (iris$Sepal.Length[[1]] > 5)
{show_text = TRUE
} else {show_text = FALSE}
```
```{r conditional_block, eval=show_text, echo=FALSE}
head(iris, 5)
```
```{r setup2, echo=FALSE, warning=FALSE, message=FALSE}
if (iris$Sepal.Length[[1]] < 5)
{show_text = TRUE
} else {show_text = FALSE}
```
```{r conditional_block2, eval=show_text, echo=FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```

Officer for Rmarkdown: Cross-referencing block_captions not numbering correctly

I'm using run_autonum and block_caption from the officer package to create figure captions for my supplemental figures and then cross-reference them (want to be a separate series than the non-supplemental figures so the numbering restarts). The numbering in the figure caption is working great (1, 2, ...). However, when cross-referencing, the number shows up as 0.
Here's a reprex Rmd file:
---
title: "Untitled"
output:
officedown::rdocx_document
---
Supplemental figure \#ref(f-s1). Supplemental figure \#ref(f-s2)
```{r, echo=FALSE}
library(officer)
sfig_num <- run_autonum(seq_id = "sfig",
pre_label = "Figure S",
bkm="f-s1")
block_caption("A figure caption.",
style = "Image Caption",
autonum = sfig_num)
sfig_num <- run_autonum(seq_id = "sfig",
pre_label = "Figure S",
bkm="f-s2")
block_caption("A figure caption.",
style = "Image Caption",
autonum = sfig_num)
```
Here's a screenshot with correct caption numbering circled in blue; incorrect cross-reference numbering circled in red.
I figured out that the numbering works if you use r run_reference(bkm-id) instead of bookdown-style \#ref(bkm-id) it works:
---
title: "Untitled"
output:
officedown::rdocx_document
---
```{r echo=FALSE, warning=FALSE}
library(officer)
library(officedown)
```
Supplemental figure `r run_reference("fs1")`. Supplemental figure `r run_reference("fs2")`
```{r, echo=FALSE, warning=FALSE}
sfig_num <- run_autonum(seq_id = "sfig",
pre_label = "Figure S",
bkm="fs1")
block_caption("A figure caption.",
style = "Image Caption",
autonum = sfig_num)
sfig_num <- run_autonum(seq_id = "sfig",
pre_label = "Figure S",
bkm="fs2")
block_caption("A figure caption.",
style = "Image Caption",
autonum = sfig_num)
```

Greek letters RMarkdown PDF plots

I'm trying to generate Greek letters in the axis labels or titles of plots in an RMarkdown file that is generating a PDF. When I run the code the editor or in the console, I see the letter fine, but when the PDF is generated, they disappear. In the short example below, I can see that the subscript operation is working but the Greek letter theta isn't present.
I've tried changing the Typeset LaTeX in PDF using: option - I have pdfLaTeX and XeLaTex - but I see no difference.
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
plot(1:100, 1:100, xlab = expression(theta[5]))
plot(1:100, 1:100, xlab = expression('theta'[5]))
```
I was using Latin modern font and came across the same problem. The following code fixed it for me. Make sure the math library for the font you are using is called for.
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, fig.showtext=TRUE}
library("showtext")
font_add("LM Roman 10", regular = "/usr/share/texmf/fonts/opentype/public/lm/lmroman10-regular.otf")
font_add("LM Roman 10", regular = "/usr/share/texmf/fonts/opentype/public/lm-math/latinmodern-math.otf")
plot(1:100, 1:100, xlab = expression(theta[5]))
```

Auto-Numbering of Figure and Table Captions for HTML Output in R Markdown

Is there a way to automatically number the figures in captions when I knit R Markdown document into an HTML format?
What I would like is for my output to be the following:
Figure 1: Here is my caption for this amazing graph.
However, I am currently getting the following:
Here is my caption for this amazing graph.
Here is my MWE:
---
title: "My title"
author: "Me"
output:
html_document:
number_sections: TRUE
fig_caption: TRUE
---
```{r setup}
knitr::opts_chunk$set(echo=FALSE)
```
```{r plot1,fig.cap="Here is my caption for this amazing graph."}
x <- 1:10
y <- rnorm(10)
plot(x,y)
```
```{r table1, fig.cap="Here is my caption for an amazing table."}
head(mtcars, 2)
```
I have read that this issue is resolved with Bookdown but I've read the Definitive Guide to Bookdown, cover to cover, and can't find it.
If you wish to have numbered figures, you will need to use an output format provided by bookdown. These include html_document2, pdf_document2 etc. See here for a more comprehensive list of options.
Changing your document example html_document to bookdown::html_document2 will resolve your problem.
---
title: "My title"
author: "Me"
output:
bookdown::html_document2:
number_sections: TRUE
fig_caption: TRUE
---
```{r setup}
knitr::opts_chunk$set(echo=FALSE)
```
```{r plot1,fig.cap="Here is my caption for this amazing graph."}
x <- 1:10
y <- rnorm(10)
plot(x,y)
```
```{r plot2, fig.cap="Here is my caption for another amazing graph."}
plot(y,x)
```
If you want to label tables created by knitr::kable, you will need to specify the caption within the table call itself
```{r table1}
knitr::kable(mtcars[1:5, 1:5], caption = "Here is an amazing table")
```

rmarkdown shows TRUE column

In an rmarkdown documents I do
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE , comment = NA, message= FALSE, warning = TRUE)
```
```{r include=TRUE, echo=TRUE, eval=TRUE, comment=TRUE, null_prefix = TRUE, message= FALSE}
library(reshape2)
#### t test
library(reshape2)
#head(tips)
t.test(tips$tip, alternative ='two.sided', mu = 2.5) ### reject null of mu = 2.5
```
and the output has "TRUE" listed as a column down next to the ouput of t.test()
How can you remove the "TRUE"?
The problem is your chunk option comment=TRUE. See the chunk options help:
comment: ('##'; character) the prefix to be put before source code
output; default is to comment out the output by ##, which is good for
readers to copy R source code since output is masked in comments (set
comment=NA to disable this feature)
What you are doing is asking every line to begin with TRUE. Since you already set comment = NA in your setup chunk, there is no need to repeat it.
```{r include=TRUE, echo=TRUE, eval=TRUE, null_prefix = TRUE, message= FALSE}
#### t test
library(reshape2)
#head(tips)
t.test(tips$tip, alternative ='two.sided', mu = 2.5) ### reject null of mu = 2.5
```