我想使用 R markdown 生成一个带有两个功能的 Word 文档
请看下面的示例(有点复杂,但它直接来自我的工作)。
---
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"))
```
由于某种原因,生成的word文件没有对数字进行编号。
这里讨论这个问题
但是有关如何更新字段的链接
https://ardata-fr.github.io/officeverse/word-documents.html#update-fields
死了。
这将在这里进一步讨论
officedown::rdocx_document 在图形和表格标题中没有显示数字
但我主要在 Linux 上工作。我将生成的 docx 文件移至 Windows 计算机,但是当我执行 CTRL + A 然后按 F9 时,我没有得到我想要的。
有什么建议吗?
又是我。我能够生成本章后面的数字:https://ardata-fr.github.io/officeverse/officedown-for-word.html#custom-cross-reference。请注意,它不使用
topcaption: true
,而是指定顺序:标题后跟数字,例如:
::: {custom-style="Table Caption"}
Figure `r run_bookmark("airquality", runs)`: State expenditure for the main co-financed State aid objectives
:::
```{r}
```
所以,它看起来像:
---
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")`
抱歉代码较长,但想强调标题位于图形环境/块之外。最后看起来像:
但即使在 libreoffice 中,我也必须打开/关闭 Viev -> 字段名称才能看到
Figure
之后的数字。希望有帮助。