FlexTable 输出 .docx 水平 table 调整为缩小边距
FlexTable output .docx horizontal table adjusted to narrow margins
我有一个名为 html_table
的 flextable
对象,我想将其直接放入 word 文档中,采用窄边距的水平布局。
我面临 2 个问题:
1) 插图中建议的方法会产生额外的页面(table 之前一页,之后一页)。我认为这是一个已知问题,但不清楚如何解决它。
2) 我希望水平页面上的边距较窄,结果 table 会自动适合页面。我想要这个,这样我就可以使用尽可能多的页面打印 table。我目前的做法是在Word上手动打开文档,更改布局和select "autofit"。
这是我用来生成文档的代码。出于说明目的,我将使用 mtcars
作为我的 table,但真实的行数多于 mtcars
。
html_table <- regulartable(mtcars)
doc <- read_docx() %>%
# Make it landscape
body_end_section_continuous() %>%
# Add the table
body_add_flextable(value = html_table,
split = TRUE
) %>%
body_end_section_landscape()
# Write the .docx
print( doc, target = "my_table.docx" )
在 Word 文档中,节仅在它们停止时才定义(我无法解释为什么这样做,但这就是底层 xml 的方式...)。如果前一节不是横向的,那么横向的部分也需要分页。
为了autofit一个flextable,使用函数autofit
.
library(flextable)
library(officer)
library(magrittr)
html_table <- regulartable(mtcars) %>%
autofit()
doc <- read_docx() %>%
body_add_flextable(value = html_table, split = TRUE) %>%
body_end_section_landscape() %>% # a landscape section is ending here
print( target = "my_table.docx" )
如果您不需要额外的页面,您将需要一个默认页面方向为横向的模板。此外,您不需要任何代码来管理方向和边距。
我有一个名为 html_table
的 flextable
对象,我想将其直接放入 word 文档中,采用窄边距的水平布局。
我面临 2 个问题:
1) 插图中建议的方法会产生额外的页面(table 之前一页,之后一页)。我认为这是一个已知问题,但不清楚如何解决它。
2) 我希望水平页面上的边距较窄,结果 table 会自动适合页面。我想要这个,这样我就可以使用尽可能多的页面打印 table。我目前的做法是在Word上手动打开文档,更改布局和select "autofit"。
这是我用来生成文档的代码。出于说明目的,我将使用 mtcars
作为我的 table,但真实的行数多于 mtcars
。
html_table <- regulartable(mtcars)
doc <- read_docx() %>%
# Make it landscape
body_end_section_continuous() %>%
# Add the table
body_add_flextable(value = html_table,
split = TRUE
) %>%
body_end_section_landscape()
# Write the .docx
print( doc, target = "my_table.docx" )
在 Word 文档中,节仅在它们停止时才定义(我无法解释为什么这样做,但这就是底层 xml 的方式...)。如果前一节不是横向的,那么横向的部分也需要分页。
为了autofit一个flextable,使用函数autofit
.
library(flextable)
library(officer)
library(magrittr)
html_table <- regulartable(mtcars) %>%
autofit()
doc <- read_docx() %>%
body_add_flextable(value = html_table, split = TRUE) %>%
body_end_section_landscape() %>% # a landscape section is ending here
print( target = "my_table.docx" )
如果您不需要额外的页面,您将需要一个默认页面方向为横向的模板。此外,您不需要任何代码来管理方向和边距。