我正在尝试使用
R
flextable
包创建 Microsoft Word 文档表。
我的尝试:
library(flextable)
library(dplyr)
# create flextables
Outer_table <- data.frame(Number = c(1,2),
table = c(NA,NA)) %>% flextable()
df1 <- data.frame(Species = c("Maple", "Oak", "Hemlock", "Red Pine"),
Age = c(40,37,52,64)) %>% flextable()
# combine flextables
Outer_table <- Outer_table %>%
flextable::compose(j ="Table", i = 1,value = df1)
这会导致错误
x$data[i,j] 中的错误 <- value: number of items to replace is not a multible of replacement length.
也许,这可以让你开始。我认为
{officer}
提供了帮助:
library(flextable)
library(officer)
# reformated data
t1 <- data.frame(Number = c(1, 2),
table = c(NA, NA)) |>
flextable() |> theme_box()
t2 <- data.frame(Species = c("Maple", "Oak", "Hemlock", "Red Pine"),
Age = c(40, 37, 52, 64)) |>
flextable() |> theme_box()
# create docx object
read_docx() |>
body_add_par("A table of sth.") |>
body_add_flextable(value = t1) |>
body_add_par("Another table of sth.") |>
body_add_flextable(value = t2) |>
print(target = "example.docx")
# you will find example.docx in your current working directory
我希望可以通过
body_add_*()
函数来实现进一步的样式,参见此处。