Is there a way to place a wide table in an rmarkdown pdf document to take advantage of the additional width of a page in landscape orientation?
I have found the following related questions on Stackoverflow which are helpful but do not address the use of the huxtable
package.
a) wide tables using different table packages: xtable
, kableExtra
, Stargazer
:
Rotate a table from R markdown in pdf
b) rotated long tables (kableExtra
):
Rotating a table in PDF output from RMarkdown with more than one page
c) rotating pdf page: Rstudio rmarkdown: both portrait and landscape layout in a single PDF
wide table in landscape pdf
The wide table works well in a pdf which is set up in landscape format.
---
title: "wide table in landscape pdf rmarkdown"
output:
pdf_document:
fig_caption: yes
geometry: margin=2cm
classoption: landscape
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(huxtable)
library(magrittr)
```
```{r wide-table, include=FALSE}
wide_table <- data.frame(id = 1:3,
g1 = c("some text", "some more text", "altogether more text"),
g2 = c("a descriptive var with lots of words", "short desc", "A typical description"),
qnt = c(2, 100, 37),
uom = c("nr", "m", "m2"),
v1 = c("1,256", NA, "123"),
v2 = c("25", "6,555", "723"),
v3 = c("12", "699", "6"),
v4 = c("9,656", "491", "6,235"),
v5 = c("564", NA, "123"),
v6 = c("278", NA, NA),
v7 = c("10", "25,365", "1524"),
v8 = c(22.4, 33.5, 45.8),
v9 = c(1.25, 45.6, 0.36))
header_names <- c("ID", "Main heading", "Some descriptive text", "A Number", "Unit", paste("Variable", 1:9))
ht <-
huxtable(wide_table) %>%
set_width(1) %>%
set_col_width(c(1/17, 2/17, 3/17, rep(1/17, 11))) %>%
set_contents(row = 1, col = 1:14, value = header_names) %>%
set_font_size(everywhere, everywhere, 8) %>%
theme_article()
```
```{r ht-wide-df, results='asis'}
ht %>%
set_caption("Wide table in landscape document")
```
Results in this which is fine: This is how I would like the table to look like in the portrait pdf with a landscape page.
wide table in portrait pdf
On a portrait page
The wide table fits into portrait orientation but headers overlap and the table begins to be difficult to read.
On a landscape page
The wide table's dimensions are the same as for the portrait orientation with the same problems:
- headers overlap and
- the table begins to be difficult to read.
The table does not stretch to fit the landscape page.
My question: is there a way to manipulate the table to fit the full space in the landscape oriented page as for the example where the document is set up in landscape in its entirety.
---
title: "wide huxtable in portrait with landscape page"
output: pdf_document
geometry: margin=2cm
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
```{r wide-tbl-port, results='asis'}
ht %>%
set_caption("Wide table in portrait document")
```
\newpage
\blandscape
```{r wide-tbl-port-ldscp-page, results='asis'}
ht %>%
set_caption("Wide table in portrait document on landscape oriented page")
```
\elandscape
Which result in the following tables: