larry77
larry77

Reputation: 1533

R+officedown: trouble with Numbering and Figure captions

I want to generate a word document using R markdown with two features

  1. Tables and Figures are numbered and can be referenced
  2. 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

https://ardata-fr.github.io/officeverse/officedown-for-word.html#caption-label-for-figures-and-tables

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?

Upvotes: 0

Views: 356

Answers (1)

Grzegorz Sapijaszko
Grzegorz Sapijaszko

Reputation: 3614

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:

::: {custom-style="Table Caption"}
Figure `r run_bookmark("airquality", runs)`: State expenditure for the main co-financed State aid objectives
:::

```{r}
```

So, it looks like:

---
title: "Table captions linked to section number"
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(officedown)
library(officer)
library(tidyverse)
library(scales)
```

## Tables
### Table example

The code below define the set of runs that will compose the auto-numbered sequence in the caption:

```{r}
runs <- list(
  run_word_field("STYLEREF 3 \\s"),
  ftext(" - "),
  run_autonum(seq_id = "tab", pre_label = "", post_label = "")
)
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"
))

```

And now we can add this sequence into a custom caption:

::: {custom-style="Table Caption"}
Figure `r run_bookmark("airquality", runs)`: State expenditure for the main co-financed State aid objectives
:::

```{r}
ggplot(
  data = cofinancing_objective_sel,
  aes(x = expenditure, y = fct_rev(top_objective))
) +
  geom_bar(position = "dodge", stat = "identity", alpha = 1, color = "black") +
  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"))
```
This is a reference to bookmark `airquality`: `r run_reference(id = "airquality")`

```{r}
runs_0 <- list(ftext("Figure :"))
runs_p <- list(ftext(": mtcars dataset"))
runs <- append(runs_0, runs)
runs <- append(runs, runs_p)
```

::: {custom-style="Table Caption"}
`r run_bookmark("mtcars", runs)`
:::

```{r}
head(mtcars)
```

 This is a reference to bookmark `mtcars`: `r run_reference(id = "mtcars")`

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.

Upvotes: 2

Related Questions