代码之家  ›  专栏  ›  技术社区  ›  Matias Andina

FlexTable输出。调整docx水平表以缩小页边距

  •  1
  • Matias Andina  · 技术社区  · 7 年前

    flextable 对象调用 html_table 我面临两个问题:

    1) 小插曲中建议的方法会产生额外的页面(表前一页,表后一页)。我认为这是一个已知的问题,但不清楚如何解决它。
    2) 我想有窄的页边空白和水平页上的表格,以自动适应页面。我想要这个,这样我就可以用尽可能多的页面打印表格。我目前的方法是手动打开文档,更改布局,然后在Word上选择“autofit”。

    这是我用来生成文档的代码。为了便于说明,我将使用 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" )
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   David Gohel    7 年前

    在Word文档中,节仅在停止时定义(我无法解释为什么这样做,但底层xml就是这样…)。另外,如果前面的部分不是面向横向的,则面向横向的部分需要分页符。

    自动装配 flextable、use函数 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" )
    

    enter image description here

    如果您不想要额外的页面,您将需要一个默认页面方向为横向的模板。此外,您不需要任何代码来管理方向和边距。