表格和图表交叉参考人员R.

问题描述 投票:2回答:1

我希望能够使用官员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个回答
3
投票

简而言之,表格编号使用{ 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

希望这可以帮助 :)

© www.soinside.com 2019 - 2024. All rights reserved.