【问题标题】:Table and Figure cross-reference officer R表格和图表交叉参考官员 R
【发布时间】:2018-09-20 19:24:36
【问题描述】:

我希望能够使用官员 R 包交叉引用 Word 文档中的表格或图形。

到目前为止,我已经遇到过这些材料,但它们似乎没有解决方案: https://davidgohel.github.io/officer/articles/word.html#table-and-image-captions 和一个类似的问题 add caption to flextable in docx

在这两个中,我只能插入一个标题作为 2 级标题,而不是真正的表格标题。

我希望在 Word 中能够做的是插入 -> 交叉引用并转到参考类型:表格并在那里查看我的标题。现在我只能看到编号项下的标题。

此功能是否存在于官员或其他任何地方?

【问题讨论】:

  • 您找到解决方案了吗?我也想要这个功能

标签: r caption flextable officer


【解决方案1】:

实际上,表编号使用{ SEQ \\@ arabic } 模式,但对它们的引用使用{ REF bookmark \h }。我们可以使用它来制作可以引用 SEQ 字段的新代码。

代码:

ft <- regulartable(head(iris)) # create flextable
str <- paste0(' REF ft \\h ')  # create string to be used as reference to future bookmark

doc <- read_docx() %>%
  body_add_par('This is my caption' , style = 'Normal') %>% # add caption
  slip_in_seqfield(str = "SEQ Table \\@ arabic",           
                   style = 'Default Paragraph Font', 
                   pos = "before") %>% # add number for table
  body_bookmark('ft') %>%   # add bookmark on the number
  slip_in_text("Table ", 
               style = 'Default Paragraph Font', 
               pos = "before") %>% # add the word 'table'
  body_add_flextable(value = ft, align = 'left') %>% # add flextable
  body_add_break() %>%  # insert a break (optional)
  slip_in_text('As you can see in Table', 
               style = 'Default Paragraph Font', 
               pos = 'after') %>% # add the text you want before the table reference
  slip_in_seqfield(str = str,  
                   style = 'Default Paragraph Font', 
                   pos = 'after') %>% # add the reference to the table you just added
  slip_in_text(', there are a lot of iris flowers.', 
               style = 'Default Paragraph Font', 
               pos = 'after') %>% # add the rest of the text 
  print('Iris_test.docx') # print

希望这会有所帮助:)

【讨论】:

  • 非常感谢!直到现在我还没有找到答案。这似乎可以解决问题:)!
  • 如果我只对在 word doc 中打印的表格感兴趣,我可以让它工作,但我如何调整它以在更大的 Rmd 文件中工作?如果我删除 'Iris_test.docx' 我可以让它在 Rmd-knitted word doc 中打印,但我得到的表格输出是内部表格结构的混乱。
  • 老实说,我从来没有在 Rmarkdown 中为我打印表格。它们对我来说总是一团糟:(如果你找到解决方案,你可以在这里发布,或者编辑我的答案并添加它?
  • 谢谢!这个有chunk/officedown版本吗?
【解决方案2】:

为了记录,您现在可以使用 {crosstable} 包中的一些辅助函数更轻松地完成此操作。

免责声明:我是该软件包的开发者,这些功能受到@morgan121 回答的极大启发。谢谢摩根!

这是一个例子:

library(officer)
library(crosstable)
library(ggplot2)
options(crosstable_units="cm")

ft = regulartable(head(iris)) 
my_plot = ggplot(data = iris ) +
  geom_point(mapping = aes(Sepal.Length, Petal.Length))

doc = read_docx() %>% 
  body_add_title("Dataset iris", 1) %>%
  body_add_normal("Table \\@ref(table_iris) displays the 6 first rows of the iris dataset.") %>%
  body_add_flextable(ft) %>%
  body_add_table_legend("Iris head", bookmark="table_iris") %>%
  body_add_normal("Let's add a figure as well. You can see in Figure \\@ref(fig_iris) that sepal length is somehow correlated with petal length.") %>%
  body_add_figure_legend("Relation between Petal length and Sepal length", bookmark="fig_iris") %>% 
  body_add_gg2(my_plot, w=14, h=10, scale=1.5)

print(doc , 'Iris_test.docx')    

更多关于https://danchaltiel.github.io/crosstable/articles/crosstable-report.html的信息。

与 morgan121 的代码一样,您必须选择 MS Word 中的所有文本并按两次 F9 才能正确更新数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多