When I create a html
document with rmarkdown
, I can flextable::autofit
table 1 below to be presented in a correct manner but when I do the same for table 2, it doesn't work, does anyone know why?
As a fix, I tried extracting the width
(using flextable_dim
) of the columns used in table 1 and used that for table 2 but it seems to ignore that argument when it is rendered.
---
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)
```
different widths:
Does anyone have a fix to make table 2 the same width as table 1 (so values in table 2 are not over two lines)? I have seen this and this but those options don't seem to work for me as the width
argument doesn't seem to change anything in the html
output.
thanks
edit: you can increase the entire width of the html document using this solution but it still doesnt answer the above question.
The long numbers you are trying to display in the table seem to be the problem. If you try reducing the font in the body, you will see this. Replace the
autofit()
line for the first table withjust as an example. You will see that now everything fits (see screenshot at the bottom).
This means: your choices are in how to display the numbers (which I find the better one) or to make reductions in the size in which you are displaying the data.