Rendering flextables to pdf in bookdown/markdown do not fit to page

Viewed 115

When I attempt to render a markdown document to pdf (using either pdf_document, or pdf_document2 from bookdown) I cannot get the included flextable (adapted from document I am referencing) to display as desired. This has been particularly frustrating as in Rstudio visual markdown editor the table displays as desired. I have attempted to use autofit() and set_table_properties(layout="autofit") to no no avail. Using the markdown script below I get tables that look like enter image description here

and

enter image description here

when my desired output which is how it appears in visual editor is enter image description here

I thought it could be due to complications with the merging of cells, borders and the theme, but even when I strip it down to a bare flextable I run into the same fitting issues. Rendering to html everything looks fine. All the posts and documentation I have found indicate it should work as written, not sure what I am missing.

---
title: "Test Flextables"
author: ""
date: ""
output:
  bookdown::pdf_document2:
    toc: false
    fig_caption: yes
    latex_engine: xelatex
    number_sections: false
    keep_tex: true
  pdf_document: default
  html_document: default
---

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

TEST_TABLE <- structure(list(`Winter Feeding Strategy` = c("Shallow snow  terrestrial  lichen", 
"Shallow snow  terrestrial  lichen", "Shallow snow  terrestrial  lichen", 
"Shallow snow  terrestrial  lichen", "Shallow snow  terrestrial  lichen", 
"Deep snow  arboreal lichen"), `Ecotype Name` = c("BC  Northern Alberta  Mountain", 
"BC  Northern Alberta  Mountain", "BC  Northern Alberta  Mountain", 
"BC  Northern Alberta  Mountain", "BC  Northern Alberta  Mountain", 
"BC  Mountain"), Location = c("Northern BC", "West central BC", 
"North central BC", "East central BC", "West central Alberta", 
"Southeastern BC"), `Nationally Significant Population by National Ecological Area (SARA)` = c("Northern Mountain", 
"Southern Mountain", "Southern Mountain", "Southern Mountain", 
"Southern Mountain", "Southern Mountain"), `COSEWIC Designatable Unit (2011)` = c("Northern Mountain", 
"Northern Mountain", "Northern Mountain", "Central Mountain", 
"Northern Mountain", "Southern Mountain"), `Southern Mountain Caribou Groupings` = c("Not Applicable", 
"Central Group", "Central Group", "Central Group", "Central Group", 
"Southern Group")), row.names = c(NA, -6L), spec = structure(list(
    cols = list(`Winter Feeding Strategy` = structure(list(), class = c("collector_character", 
    "collector")), `Ecotype Name` = structure(list(), class = c("collector_character", 
    "collector")), Location = structure(list(), class = c("collector_character", 
    "collector")), `Nationally Significant Population by National Ecological Area (SARA)` = structure(list(), class = c("collector_character", 
    "collector")), `COSEWIC Designatable Unit (2011)` = structure(list(), class = c("collector_character", 
    "collector")), `Southern Mountain Caribou Groupings` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"),  class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))




```

## Test Table No Fitting

```{r TEST1, echo=FALSE, message=FALSE, warning=FALSE}



TEST_FLEX<- flextable(TEST_TABLE) %>% 
  merge_v(1:6)%>% 
  theme_zebra() %>% 
  border_inner_h() 
  
TEST_FLEX



```

## Test Table autofit()

```{r TEST2, echo=FALSE, message=FALSE, warning=FALSE}





TEST_FLEX<- flextable(TEST_TABLE) %>% 
  merge_v(1:6)%>% 
  theme_zebra() %>% 
  border_inner_h() 
  
autofit(TEST_FLEX)



```

## Test Table set_table_properties

```{r TEST3, echo=FALSE, message=FALSE, warning=FALSE}





TEST_FLEX<- flextable(TEST_TABLE) %>% 
  merge_v(1:6)%>% 
  theme_zebra() %>% 
  border_inner_h() 
  
set_table_properties(TEST_FLEX, layout="autofit")



```


2 Answers

Sorry, this was a bug. It should be fixed on Github (and soon available on CRAN).

---
title: "Test Flextables"
author: ""
date: ""
output:
  bookdown::pdf_document2:
    toc: false
    fig_caption: yes
    latex_engine: xelatex
    number_sections: false
    keep_tex: true
  pdf_document: default
  html_document: default
---

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

TEST_TABLE <- structure(list(`Winter Feeding Strategy` = c("Shallow snow  terrestrial  lichen", 
"Shallow snow  terrestrial  lichen", "Shallow snow  terrestrial  lichen", 
"Shallow snow  terrestrial  lichen", "Shallow snow  terrestrial  lichen", 
"Deep snow  arboreal lichen"), `Ecotype Name` = c("BC  Northern Alberta  Mountain", 
"BC  Northern Alberta  Mountain", "BC  Northern Alberta  Mountain", 
"BC  Northern Alberta  Mountain", "BC  Northern Alberta  Mountain", 
"BC  Mountain"), Location = c("Northern BC", "West central BC", 
"North central BC", "East central BC", "West central Alberta", 
"Southeastern BC"), `Nationally Significant Population by National Ecological Area (SARA)` = c("Northern Mountain", 
"Southern Mountain", "Southern Mountain", "Southern Mountain", 
"Southern Mountain", "Southern Mountain"), `COSEWIC Designatable Unit (2011)` = c("Northern Mountain", 
"Northern Mountain", "Northern Mountain", "Central Mountain", 
"Northern Mountain", "Southern Mountain"), `Southern Mountain Caribou Groupings` = c("Not Applicable", 
"Central Group", "Central Group", "Central Group", "Central Group", 
"Southern Group")), row.names = c(NA, -6L), spec = structure(list(
    cols = list(`Winter Feeding Strategy` = structure(list(), class = c("collector_character", 
    "collector")), `Ecotype Name` = structure(list(), class = c("collector_character", 
    "collector")), Location = structure(list(), class = c("collector_character", 
    "collector")), `Nationally Significant Population by National Ecological Area (SARA)` = structure(list(), class = c("collector_character", 
    "collector")), `COSEWIC Designatable Unit (2011)` = structure(list(), class = c("collector_character", 
    "collector")), `Southern Mountain Caribou Groupings` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"),  class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))
```

## Test Table No Fitting

```{r TEST1, echo=FALSE, message=FALSE, warning=FALSE}
TEST_FLEX<- flextable(TEST_TABLE) %>% 
  merge_v(1:6)%>% 
  theme_zebra() %>% 
  border_inner_h() 
  
TEST_FLEX
```

enter image description here

If you're willing to tolerate a huxtable-based solution (my package), is this close to what you want?

library(huxtable)
tth <- as_hux(TEST_TABLE) |> 
         merge_repeated_rows(everywhere, 1:6) |> 
         set_tb_borders() |> 
         set_width("6in") |> 
         set_col_width(c(.6/5, .6/5, .6/5, .4,.6/5, .6/5)) 

where quick_pdf(tth, width = "7in", height = "4in") looks like the below:

PDF output from huxtable

You can customize output further with huxtable commands, e.g. setting background colors for your header, or adding a stripe with map_background_color(tth, by_rows(...)).

Related