I want to generate a word document using R markdown with two features
- Tables and Figures are numbered and can be referenced
- the figure caption goes on top of the figure.
Please have a look at the example below (it is a bit convoluted, but it comes straight from my work).
---
output:
officedown::rdocx_document:
plots:
topcaption: true
---
```{r, scoreboard, echo=FALSE, eval=TRUE}
options( scipen = 16 )
options(tidyverse.quiet = TRUE)
options(message = FALSE)
library(tidyverse, warn.conflicts = FALSE)
library(officedown)
library(scales)
cofinancing_objective_sel <- structure(list(top_objective = structure(1:6, levels = c("Environmental protection, including energy savings",
"Agriculture, Forestry, and Rural areas", "SMEs including risk capital",
"Research, development and innovation", "Regional development",
"Other"), class = "factor"), expenditure = c(73.4519, 69.1646,
51.6918, 24.2113, 17.2887, 4.3701)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
```
```{r myfigure1, fig.cap="State expenditure for the main co-financed State aid objectives", echo=FALSE, fig.width=6, fig.height=3.7}
ggplot(data = cofinancing_objective_sel,
aes(x = expenditure, y=fct_rev(top_objective))) +
geom_bar(position="dodge", stat="identity", alpha=1, color="black") +
## scale_x_continuous(labels = label_percent())+
scale_y_discrete(labels = wrap_format(20))+
xlab("State aid expenditure (million EUR)")+
ylab(NULL)+
labs(title=paste("Top State aid objectives for co-financed aid in 2022"))
```
```{r myfigure2, fig.cap="State expenditure for the main co-financed State aid objectives", echo=FALSE, fig.width=6, fig.height=3.7}
ggplot(data = cofinancing_objective_sel,
aes(x = expenditure, y=fct_rev(top_objective))) +
geom_bar(position="dodge", stat="identity", alpha=1, color="black") +
theme_minimal() +
## scale_x_continuous(labels = label_percent())+
scale_y_discrete(labels = wrap_format(20))+
xlab("State aid expenditure (million EUR)")+
ylab(NULL)+
labs(title=paste("Top State aid objectives for co-financed aid in 2022"))
```
For some reason, the generated word file does not number the figures.
This is discussed here
but the link about how to update the fields
https://ardata-fr.github.io/officeverse/word-documents.html#update-fields
is dead.
This is further discussed here
officedown::rdocx_document showing no number in figure and table captions
but I work primarily on linux. I moved the generated docx file to a windows machine, but when I do CTRL + A followed by F9, I do not get what I want.
Any suggestions?
Me again. I was able to generate the numbers following this chapter: https://ardata-fr.github.io/officeverse/officedown-for-word.html#custom-cross-reference. Please note, it doesn't use
topcaption: true, instead it specify the order: caption followed by figure, like:So, it looks like:
Sorry for long part of code, but wanted to highlight that caption is outside of figure environment/chunk. Finally it looks like:
But even in libreoffice I had to switch on/off Viev -> Field names to see numbers after
Figure. Hope it helps.