rmarkdown html 文档的 flextable 宽度

问题描述 投票:0回答:1

当我使用

html
创建
rmarkdown
文档时,我可以
flextable::autofit
下面的表 1 以正确的方式呈现,但是当我对表 2 执行相同操作时,它不起作用,有人知道为什么吗?

作为修复,我尝试提取表 1 中使用的列的

width
(使用
flextable_dim
)并将其用于表 2,但在渲染时似乎忽略了该参数。

---
title: "tmp"
output: html_document
---


```{r, include=FALSE, cache=FALSE, results = "hide", fig.keep = "none"}
library(flextable)
library(officer)
library(tidyverse)

df1 <- structure(list(Variable = "this is a variable name for table 1", year = "", 
    `1` = "3221605", `2` = "3978169", `3` = "3398549", 
    `4` = "3339557", `5` = "3540978", `6` = "3480641", 
    `7` = "3454676", `8` = "3437130", `9` = "3640963", 
    `10` = "3891966", `11` = "3873802", `12` = "3834393", 
    `13` = "3088655", `14` = "3280407", `15` = "3505542"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))


df2 <- structure(list(Variable = "this is a variable name for table 2", 
    place_holder = "", w1 = "2.1 (1-4)", w2 = "2.1 (1-4)", 
    w3 = "1.2 (1-4)", w4 = "2 (1-4)", w5 = "1 (1-4)", 
    w6 = "1 (1-3)", w7 = "1 (1-3)", w8 = "1 (1-3)", 
    w9 = "1 (1-3)", w10 = "1 (1-3)", w11 = "1 (1-4)", 
    w12 = "2 (1-4)", w13 = "2 (1-5)", w14 = "2 (1-4)", 
    w15 = "2 (1-4)"), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"))
```



table 1

```{r tab1, include = TRUE, echo = FALSE, eval = T, warning = FALSE, message = FALSE}
header_names <- setNames(c("Variable", "level", 1990:2014),
                         names(df1))
ft1 <- flextable(df1) %>% 
  align_text_col(align = "center") %>% 
  align(i = NULL, j = 1, align = "left", part = "body") %>% 
  bold(part = "header") %>%  # bold header
  set_header_labels(values = header_names) %>% 
  bold(j = c(1, 2)) %>% 
  autofit() 
ft1
flextable_dim(ft1)
```

table 2

```{r tab2, include = TRUE, echo = FALSE, eval = T, warning = FALSE, message = FALSE}
header_names <- setNames(c("Variable", "level", 1990:2014),
                         names(df2))
ft2 <- flextable(df2) %>% 
  align_text_col(align = "center") %>% 
  align(i = NULL, j = 1, align = "left", part = "body") %>% 
  bold(part = "header") %>%  # bold header
  set_header_labels(values = header_names) %>% 
  bold(j = c(1, 2)) %>% 
  autofit()
  #width of each column in table 1
  #16.51252/17 = 0.9713247
  #width(width = 0.9713247)
  #set_table_properties(layout = "autofit") 
ft2
#flextable_dim(ft)
```

table 2 with width of table 1:

```{r tab2-1, include = TRUE, echo = FALSE, eval = T, warning = FALSE, message = FALSE}
header_names <- setNames(c("Variable", "level", 1990:2014),
                         names(df2))
ft2 <- flextable(df2) %>% 
  align_text_col(align = "center") %>% 
  align(i = NULL, j = 1, align = "left", part = "body") %>% 
  bold(part = "header") %>%  # bold header
  set_header_labels(values = header_names) %>% 
  bold(j = c(1, 2)) %>% 
  #autofit()
  #width of each column in table 1
  #16.51252/17 = 0.9713247
  width(width = 0.9713247)
  #set_table_properties(layout = "autofit") 
ft2
#flextable_dim(ft)
```

不同宽度:

是否有人有办法使表 2 与表 1 具有相同的宽度(因此表 2 中的值不超过两行)?我已经看到了 thisthis 但这些选项似乎对我不起作用,因为

width
参数似乎没有改变
html
输出中的任何内容。

谢谢

编辑:您可以使用此解决方案增加html文档的整个宽度,但它仍然不能回答上述问题。

r r-markdown flextable officer
1个回答
0
投票

您尝试在表格中显示的长数字似乎是问题所在。如果您尝试减小正文中的字体,您会看到这一点。将第一个表的

autofit()
行替换为

  fontsize(size = 7, part = "body")

仅作为示例。您会看到现在一切都合适(请参见底部的屏幕截图)。

这意味着:您的选择是如何显示数字(我认为这是更好的)或缩小显示数据的大小。



© www.soinside.com 2019 - 2024. All rights reserved.