用officer包写word文档:如何合并多个rdocx对象?
Writing word documents with the officer package: How to combine several rdocx objects?
我有几个使用 R 中的 officer package 创建的 rdocx 数据对象。我想将这些 rdocx 对象组合成一个 rdocx 对象。
考虑以下示例:
library("officer")
# Create two rdocx data objects
doc1 <- read_docx()
doc1 <- doc1 %>%
body_add_par("doc1_aaa", style = "Normal") %>%
body_add_par("doc1_bbb", style = "Normal") %>%
body_add_par("doc1_ccc", style = "Normal")
doc2 <- read_docx()
doc2 <- doc2 %>%
body_add_par("doc2_aaa", style = "Normal") %>%
body_add_par("doc2_bbb", style = "Normal") %>%
body_add_par("doc2_ccc", style = "Normal")
如何合并这两个 rdocx 对象,使 Word 中的最终输出如下所示?
# Expected output
doc3 <- # combine doc1 and doc2
print(doc3, target = "Final-Output.docx")
# doc1_aaa
# doc1_bbb
# doc1_ccc
# doc2_aaa
# doc2_bbb
# doc2_ccc
您将需要函数 body_add_docx
:
library(officer)
library(magrittr)
doc2 <- read_docx()
doc2 <- doc2 %>%
body_add_par("doc2_aaa", style = "Normal") %>%
body_add_par("doc2_bbb", style = "Normal") %>%
body_add_par("doc2_ccc", style = "Normal") %>% print(target = "doc2.docx")
doc1 <- read_docx()
doc1 <- doc1 %>%
body_add_par("doc1_aaa", style = "Normal") %>%
body_add_par("doc1_bbb", style = "Normal") %>%
body_add_par("doc1_ccc", style = "Normal") %>%
body_add_docx(src = "doc2.docx") %>%
print(target = "Final-Output.docx")
我有几个使用 R 中的 officer package 创建的 rdocx 数据对象。我想将这些 rdocx 对象组合成一个 rdocx 对象。
考虑以下示例:
library("officer")
# Create two rdocx data objects
doc1 <- read_docx()
doc1 <- doc1 %>%
body_add_par("doc1_aaa", style = "Normal") %>%
body_add_par("doc1_bbb", style = "Normal") %>%
body_add_par("doc1_ccc", style = "Normal")
doc2 <- read_docx()
doc2 <- doc2 %>%
body_add_par("doc2_aaa", style = "Normal") %>%
body_add_par("doc2_bbb", style = "Normal") %>%
body_add_par("doc2_ccc", style = "Normal")
如何合并这两个 rdocx 对象,使 Word 中的最终输出如下所示?
# Expected output
doc3 <- # combine doc1 and doc2
print(doc3, target = "Final-Output.docx")
# doc1_aaa
# doc1_bbb
# doc1_ccc
# doc2_aaa
# doc2_bbb
# doc2_ccc
您将需要函数 body_add_docx
:
library(officer)
library(magrittr)
doc2 <- read_docx()
doc2 <- doc2 %>%
body_add_par("doc2_aaa", style = "Normal") %>%
body_add_par("doc2_bbb", style = "Normal") %>%
body_add_par("doc2_ccc", style = "Normal") %>% print(target = "doc2.docx")
doc1 <- read_docx()
doc1 <- doc1 %>%
body_add_par("doc1_aaa", style = "Normal") %>%
body_add_par("doc1_bbb", style = "Normal") %>%
body_add_par("doc1_ccc", style = "Normal") %>%
body_add_docx(src = "doc2.docx") %>%
print(target = "Final-Output.docx")