Rmarkdown 中的 flextable 自动调整到 word doc 导致表格超出页边距 [英] flextable autofit in a Rmarkdown to word doc causes table to go outside page margins

查看:138
本文介绍了Rmarkdown 中的 flextable 自动调整到 word doc 导致表格超出页边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rmarkdown .doc 文档中使用 flextable 来格式化我的表格.我喜欢 flextable 提供的简单格式化选项(但对其他类似的包开放),但发现我认为应该是基本功能的东西并不总是有效.

Hi I am trying to use flextable in an Rmarkdown .doc document to format my tables. I like the simple formatting options that flextable gives (but open to other similar packages) but have found something that I thought should be a basic function doesn't always work.

我想使表格适合 Word 文档的宽度,以便在需要时将文本换行在一列中以适合,但如果页面上有可用空间,则列将变宽,但不会达到此程度列太宽以至于未显示所有数据.

I want to fit the table to the width of the word document so that if required text will wrap within a column to fit, but if there is free space available on the page columns will be made wider, but not to the extent that the column is so wide that not all data is shown.

我在下面做了一个例子来说明我的问题.默认情况下,所有列都具有相同的宽度,并且文本会被换行以适应,但因此会浪费空间.我想使列适合数据(使用 autofit),但这会使列如此宽,以至于它们会从屏幕上消失.我希望 mpgvscarb 更宽一些,但仍然有一些文本换行.例如.我想要这个:

I have made an example below to show my problem. By default all columns are the same width and text is wrapped to fit, but there is therefore space wasted. I want to fit columns to the data (using autofit) but this then makes the columns so wide they go off the screen. I'm hoping for a happy-medium with mpg, vs and carb wider but some text wrapping still happening. E.g. I want this:

显然我可以使用 flextable::width 手动更改宽度,但我的表格是通过自动过程制作的,所以我不知道我希望每列有多宽.

Obviously I can manually change widths using flextable::width but my table is made through an automated process so I don't know how wide I want each column to be.

这是我的示例,它显示了 autofit

Here is my example which shows the problems either with or eithout autofit

---
title: "TestTable"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


suppressWarnings(suppressPackageStartupMessages({library(knitr)
  library(pander)
  library(flextable)
  library(tidyverse)
library(flextable)}))
```

## Normal Table
Without extra formatting, wraps text so that it fits on the page, but there is no need for column widths to be equal and could be wider on the page

```{r, echo=FALSE, results = 'asis' }
mydata <- mtcars %>% head(3) %>% select(mpg, cyl, hp,vs, gear, carb) %>%
  mutate(mpg = paste0(mpg, "with extra stuff to make long"),
         vs = paste0(vs, "+ extra stuff"),
         carb = paste0(carb, "also with extra stuff to make longer"))

mytable <- mydata  %>% flextable(theme_fun = theme_box)

mytable

```
## Table With autofit  
But table with autofit goes off the edges of the page


```{r, echo=FALSE, results = 'asis' }

mytable %>% autofit()

```


## Table With autofit and manual breaks
And if manual breaks are inserted into the column autofit makes the columns too wide an they still go off the edges of the page

```{r, echo=FALSE, results = 'asis' }

mydataWithBreaks <- mtcars %>% head() %>% select(mpg, cyl, hp,vs, gear, carb) %>%
  mutate(mpg = paste0(mpg, "\nwith extra stuff\n to make long"),
         vs = paste0(vs, "+\n extra stuff"),
         carb = paste0(carb, "\nalso with extra stuff\n to make longer"))

mydataWithBreaks  %>% flextable(theme_fun = theme_box)%>% autofit()



```

推荐答案

我已经编写了一个函数来使表格适合页面,目前效果很好,尽管我仍然有点惊讶没有内置函数这样做会(例如)知道页面本身的宽度,所以如果有人知道任何仍然渴望听到的.

I have written a function to fit the table to the page which works well for now, although I'm still a bit surprised that there is no built in function to do this which would (for example) know the width of the page itself, so if any one knows any still keen to hear.

FitFlextableToPage <- function(ft, pgwidth = 6){

  ft_out <- ft %>% autofit()

  ft_out <- width(ft_out, width = dim(ft_out)$widths*pgwidth /(flextable_dim(ft_out)$widths))
  return(ft_out)
}

这篇关于Rmarkdown 中的 flextable 自动调整到 word doc 导致表格超出页边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆