1

我想用{officedown}.docx创建一个报告,但不是使用“书签”引用类型来交叉引用用户文档ch.4.6ch.4.7中建议的表格和图形,我希望我的引用是结果中的实际“Figure”和“Table”引用类型。正如在此 stackoverflow 帖子中所讨论的,这将需要和word 字段,并且答案显示了如何通过or来实现此功能。.docx{SEQ Figure \\* arabic}{SEQ Table \\* arabic}officer::slip_in_seqfield()crosstable::body_add_table_legend()

但是,我认为,这些获取实际“Figure”和“Table”引用类型的方法仅适用于 {officer} 语法,而不适用于 {officedown} 语法。说清楚,到目前为止我明白了

  • {officer} 语法作为 R 脚本,使用以[示例]dplyr开头的链read_docx() %>%
  • {officedown} 语法作为 Rmd 脚本,在块内使用block_caption()和使用run_autonum()[示例]

(如果我错了请纠正我)

因此,我想知道是否有办法修改 {officedown} 中建议的方法,以获得实际的“图形”和“表格”引用类型,我也可以在文本中交叉引用。以下是使用标准建议方法的示例:

You can also make use of the `run_autonum()` and `block_caption()` functions 
of the {officer} package. Here are the cross references to Table \@ref(tab:tabmtcars2) 
and Figure \@ref(fig:figmtcars2). This approach can be used for tables and figures 
and the corresponding reference type in MS Word will always be "bookmark".

```{r}
# Table
run_autonum(seq_id = "tab",
            bkm = "tabmtcars2") %>%
  block_caption(autonum = .,
                label = "mtcars table",
                style = "Table Caption")

head(mtcars)

# Figure
ggplot(head(mtcars), aes(x = mpg, y = hp)) + 
  geom_point() + theme_bw()

run_autonum(seq_id = "fig",
            bkm = "figmtcars2") %>%
  block_caption(autonum = .,
                label = "mtcars figure",
                style = "Image Caption")
```

在此处输入图像描述

4

2 回答 2

3

这是我第一次在这里发帖,如果我得到格式化/等,很抱歉。不正确。但如果我理解正确,officer::run_reference 可能就是你要找的。Thomas de Marchin在这里给出了一个非常有用的例子。

其本质是在你的r代码块中使用block_caption和officer::run_autonum,然后在markdown中使用officer::run_reference代替\@ref(tab:some-tab),如下所示:

```{r}
block_caption(label = 'The Caption',
              style = 'Table Caption',
              autonum = officer::run_autonum(seq_id = 'tab', bkm = 'some-tab'))

head(mtcars) %>% flextable()
```
 This is a reference to Table `r officer::run_reference('some-tab')`

于 2021-08-03T16:03:01.420 回答
1

正如您在 crosstable 的小插图 ( https://danchaltiel.github.io/crosstable/articles/crosstable-report.html )中所读到的,您可以使用以下语法:

---
title: "mtcars"
output: bookdown::word_document2
---
    
```{r setup, include=FALSE}
library(crosstable)
library(flextable)
library(ggplot2)
```

Table iris is given in Table \@ref(tab:tabmtcars2).

```{r tbl, echo=FALSE, results='asis'}
cat("<caption> (\\#tab:tabmtcars2) Table Iris </caption> \n\r ")
head(mtcars) %>% flextable
```


A figure about mtcars is given in Figure \@ref(fig:tabmtcars).

```{r fig, echo=FALSE, results='asis'}
cat("<caption> (\\#fig:tabmtcars) Figure of mtcars heard </caption> \n\r ")
ggplot(head(mtcars), aes(x = mpg, y = hp)) + 
  geom_point() + theme_bw()
```

编号将是正确的,但它不会创建任何 MS Word 字段,因此您将无法在图形摘要中包含这些书签。

我不知道使用 Rmarkdown 包含此类字段的任何方法,但您可以在纯 R 脚本中使用官员语法(在同一链接中描述)。

于 2021-07-29T11:30:26.547 回答