I am using bookdown to create pdf reports, but my tables are all floating down to the bottom of the page, regardless of how much space there is. See this example:
---
title: "test_doc"
author: "Jake Thompson"
date: "6/30/2017"
output:
bookdown::pdf_document2:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, collapse = TRUE)
library(tidyverse)
```
# Test heading
Let make a data frame and print it in Table \#ref(tab:test-table)
```{r test-table}
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
knitr::kable(caption = "This is a test")
```
The resulting pdf looks like this:
Why does the table go to the bottom of the page? And is there a way to prevent this behavior?
You can solve this problem with kableExtra by
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
knitr::kable(caption = "This is a test") %>%
kableExtra::kable_styling(latex_options = "hold_position")
It basically insert a [!h] to the LaTeX table environment which will prevent the floating behavior and pin the table at current location.
I had to use
kable_styling(latex_options = "HOLD_position")
Note the uppercase HOLD_position, different from hold_position. See also here.
To be able to use that, I also had to add to the top section of the doc (from How to build a latex kable through bookdown::render_book?):
output:
pdf_document:
extra_dependencies: ["float"]
Related
I am having some issues with a table built on Rmarkdown to PDF output.
I am using gtsummary to summarise stratified data, then using to_kable_extra in order for export to PDF.
My code looks like this
title: "Untitled"
output:
pdf_document:
keep_tex: yes
date: "2022-08-17"
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gtsummary)
library(kableExtra)
library(dplyr)
```
```{r}
data <- data.frame(id = rep(1:3), Age = c(20,20,25),Area = letters[1:3])
data %>%select(-id) %>%tbl_strata(strata = Area, .tbl_fun = ~.x %>% tbl_summary(missing = 'no'))%>%
as_kable_extra(
booktabs = TRUE,
longtable = TRUE,
linesep = ""
) %>%
kableExtra::kable_styling(
position = "left",
latex_options = c("striped", "repeat_header"),
stripe_color = "gray!15"
)%>% column_spec(column = 1:4, width = '4cm')
```
The problem comes on the Latex output, with "column_spec" on R i can set latex columns width without issue.
However the multicolumns created for the stratified data I can not from R set their width, and that is my question. How can I achieve this without having to edit the .tex file by myself everytime I want to get this report done.
This is a piece of the Latex file, instead of what is actually happening : \multicolumn{1}{c}{\textbf{a}}; I need it to be \multicolumn{1}{p{4cm}}{\textbf{a}}
\begin{longtable}[l]{>{\raggedright\arraybackslash}p{4cm}>
{\centering\arraybackslash}p{4cm}>{\centering\arraybackslash}p{4cm}>
{\centering\arraybackslash}p{4cm}}
\toprule
\multicolumn{1}{c}{ } & \multicolumn{1}{c}{\textbf{a}} & \multicolumn{1}{c}{\textbf{b}}
& \multicolumn{1}{c}{\textbf{c}} \\
Another possible solution for me would be to turn this multicolumns information to a regular row record susceptible to general column width.
Let's say I have an Rmarkdown document.
In that document, I create an data frame with two columns, and each of them is quite long. Also I have the "paged" output setting.
---
title: "Long Tables"
output:
html_document:
toc: true
toc_depth: 3
df_print: paged
---
```{r}
alphabet = paste(LETTERS, letters, collapse = "", sep = "")
data.frame(
a = rep(alphabet, 10),
b = rep(alphabet, 10)
)
When I knit this to HTML, it appears like this:
It is important that I can get both columns to fit on screen without the user having to click through to each column. Ideally there would be an rmarkdown setting to solve this. If not, is there a way to actually truncate the columns behind the scenes, but without actually showing the user the code that is doing the truncation? Because this will complicate the example that I am demonstrating in the document.
Try this solution:
Your data:
```{r, echo=FALSE}
alphabet = paste(LETTERS, letters, collapse = "", sep = "")
df <- data.frame(
a = rep(alphabet, 10),
b = rep(alphabet, 10)
)
```
Your table:
```{r, echo=FALSE, warning=FALSE}
library(DT)
datatable(df, extensions = 'FixedColumns')
```
P.S. How to customize your table further - you can find a lot of interesting info there
A different solution is setting the style (CSS) for the table. This means that you can determine your own width of the table. So this code is behind the scenes, because when you open the html, you actually don't see the CSS code. You can use the following code:
---
title: "Long Tables"
output:
html_document:
toc: true
toc_depth: 3
df_print: paged
---
<div style="width = 100%">
```{r}
alphabet = paste(LETTERS, letters, collapse = "", sep = "")
df <- data.frame(
a = rep(alphabet, 10),
b = rep(alphabet, 10)
)
DT::datatable(df)
```
</div>
Output looks like this:
I am producing a pdf document using bookdown. In the document I have a table and in some of the cells in the table I have png images.
I can successfully build the book, but the alignment of figures and text does not match. It looks like all the text is aligned to the bottom of the cells whereas the images are aligned to the top of the cells.
I have tried using the pander package instead of kableExtra and got the exact same result.
Here is an example:
---
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
toc: false
includes:
delete_merged_file: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, cache=TRUE)
library(kableExtra)
library(knitr)
```
# Hello World
I think the world is flat
```{r}
flag = "http://flagpedia.net/data/flags/mini/gb.png"
download.file(flag,'flag.png', mode = 'wb')
tbl_img <- data.frame(
col1 = c("row1", "row2"),
col2 = c("",
""),
col3 = c(
"here is a whole bunch of text to show how the images and text in the table are not properly aligned.",
"here is a whole bunch of text to show how the images and text in the table are not properly aligned."))
tbl_img %>%
kbl(booktabs = T) %>%
column_spec(2, image = spec_image(
c("flag.png", "flag.png"), 600, 400)) %>%
column_spec(3, width = "5cm")
```
Which produces this:
Is it possible to get the text in columns 1 and 3 to be vertically aligned in their cells (text starting in the top left of the cell)?
I have used the workaround described by #Lyngbakr in the past kable: Vertical alignment does not work with pdf output
---
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
toc: false
header-includes:
- \usepackage[export]{adjustbox}
delete_merged_file: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, cache=TRUE)
library(kableExtra)
library(knitr)
```
# Hello World
I think the world is flat
```{r}
flag = "http://flagpedia.net/data/flags/mini/gb.png"
download.file(flag,'flag.png', mode = 'wb')
tbl_img <- data.frame(
col1 = c("row1", "row2"),
col2 = c("\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}",
"\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}"),
col3 = c(
"here is a whole bunch of text to show how the images and text in the table are not properly aligned.",
"here is a whole bunch of text to show how the images and text in the table are not properly aligned."))
tbl_img %>%
kbl(booktabs = T, escape = F) %>%
kable_styling(full_width = T)
```
When I embed a table generated by using knitr::kable,
I want to adjust the width of the table in beamer_presentation's slide.
Like this picture, some columns don't show in slide when executing the following codes.
Could you tell me how to solve this problem without changing column's names?
---
title: "test"
author: "test"
date: "`r Sys.time()`"
output:
beamer_presentation:
latex_engine: xelatex
fontsize: 6pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(include = FALSE)
library(tidyverse)
library(kableExtra)
library(knitr)
```
## Table
```{r test, results = "asis",include=TRUE}
test_tbl = tibble(
example_name1 = c("a", "b"),
example_name2 = c("aa", "bb"),
example_name3 = c("aaa", "bbb"),
example_name4 = c("aaaa", "bbbb"),
example_name5 = c("aaaaa", "bbbbb")
)
knitr::kable(test_tbl, format = "markdown")
```
The table is too large to fit on one slide. By adjusting the font size with kableExtra the size of the table can be changed. To do this, set the argument format = "latex" in kable() and then pipe kable_styling().
knitr::kable(test_tbl, format = "latex") %>%
kableExtra::kable_styling(font_size = 7)
It is useful to include LaTeX packages like booktabs to format tables nicely with kable or kableExtra. This is not as flexible as working with LaTeX directly but often leads to quite good results.
First of all, include booktabs in the YAML header.
---
title: "test"
author: "test"
date: "`r Sys.time()`"
header-includes:
- \usepackage{booktabs}
output: beamer_presentation
---
Then set booktabs = TRUE in kable().
knitr::kable(test_tbl, format = "latex", booktabs = T) %>%
kableExtra::kable_styling(font_size = 7)
I am using bookdown to create pdf reports, but my tables are all floating down to the bottom of the page, regardless of how much space there is. See this example:
---
title: "test_doc"
author: "Jake Thompson"
date: "6/30/2017"
output:
bookdown::pdf_document2:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, collapse = TRUE)
library(tidyverse)
```
# Test heading
Let make a data frame and print it in Table \#ref(tab:test-table)
```{r test-table}
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
knitr::kable(caption = "This is a test")
```
The resulting pdf looks like this:
Why does the table go to the bottom of the page? And is there a way to prevent this behavior?
You can solve this problem with kableExtra by
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
knitr::kable(caption = "This is a test") %>%
kableExtra::kable_styling(latex_options = "hold_position")
It basically insert a [!h] to the LaTeX table environment which will prevent the floating behavior and pin the table at current location.
I had to use
kable_styling(latex_options = "HOLD_position")
Note the uppercase HOLD_position, different from hold_position. See also here.
To be able to use that, I also had to add to the top section of the doc (from How to build a latex kable through bookdown::render_book?):
output:
pdf_document:
extra_dependencies: ["float"]