我正在尝试使用 R flextable 包创建 Microsoft Word 文档表,如下所示: 如果我有类似的东西:
library(flextable)
# create flextables
Outer_table <- data.frame(Number = c(1,2), table = c(NA,NA) %<%
flex_table()
df1 <- data.frame(Species = c("Maple", "Oak", "Hemlock", "Red Pine"),Age=c(40,37,52,64)) %<%
flex_table()
#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." What would be a way of properly fomrating the smaler dfs to create a table such as this ?另外,我如何在表格上方添加文本“按物种划分的平均树龄表”?
也许,这可以让你开始。我认为
{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_*()
函数来实现进一步的样式,参见此处。