Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown Kable width issues

Tags:

r-markdown

I am trying to create a table to hold some basic information using Kable in RMarkdown that will be generated in HTML, PDF, and Word. Here is the code I have

---
title: "test"
author: ''
date: "2015/03/24"
output:
 pdf_document:
 keep_tex: yes  
---

```{r kable1, echo=FALSE}
Variable <- c("VAR1", "VAR2", "VAR3", "VAR4")
Label <- c("LABEL", "A very       loooooooooooooooooooooooooooooooooooooooooooo     ooooooooooooooooooooooooooooooooooooooo nnnnnnngggggggg label yall", "LAB3", "LAB4")
Classification <- c("Type1", "Type2", "Type1", "Type1")
data <- data.frame(Variable, Label, Classification)
library(knitr)
kable(data)
```

```{r kable2, echo=FALSE}
Variable <- c("VAR1", "VAR2", "VAR3", "VAR4")
Label <- c("LABEL", "LabLE", "LAB3", "LAB4")
Classification <- c("Type1", "Type2", "Type1", "Type1")
data <- data.frame(Variable, Label, Classification)
library(knitr)
kable(data)
```

The HTML output is as follows. enter image description here This is what I want. I like how the table fills out the html. However, when I produce PDF I get the following. enter image description here

As we can see there are issues with the PDF, the first table runs off the page and the second does not fill up the entire width. I am, unfortunately, a n00b when it comes to R, Kable, and RMarkdown. Is there a way to set options on the kable table so that the PDF looks as nice as the HTML in terms of page placement and width? Thanks!

like image 862
decal Avatar asked Sep 29 '22 11:09

decal


1 Answers

LaTeX will not break the long lines inside of a table cell for you -- or you should use a custom environment for that, which can be sometimes really frustrating when writing markdown to be processed by Pandoc. That's why I came up with the idea to break long lines inside of the cells before transforming to markdown via the pander package. Quick example:

> pander(data, split.cells = 30, split.table = Inf)

-------------------------------------------------------------------------
 Variable                      Label                      Classification 
---------- --------------------------------------------- ----------------
   VAR1                        LABEL                          Type1      

   VAR2                       A very                          Type2      
           loooooooooooooooooooooooooooooooooooooooooooo                 
              ooooooooooooooooooooooooooooooooooooooo                    
                    nnnnnnngggggggg label yall                           

   VAR3                        LAB3                           Type1      

   VAR4                        LAB4                           Type1      
-------------------------------------------------------------------------

Or if you will have several tables with the same problem, specify the split value once:

> panderOptions('table.split.table', Inf)
> pander(data)

-------------------------------------------------------------------------
 Variable                      Label                      Classification 
---------- --------------------------------------------- ----------------
   VAR1                        LABEL                          Type1      

   VAR2                       A very                          Type2      
           loooooooooooooooooooooooooooooooooooooooooooo                 
              ooooooooooooooooooooooooooooooooooooooo                    
                    nnnnnnngggggggg label yall                           

   VAR3                        LAB3                           Type1      

   VAR4                        LAB4                           Type1      
-------------------------------------------------------------------------

In short, look for the table.split.table and table.split.cells global options, although there are a bunch of other useful tweaks as well.

like image 104
daroczig Avatar answered Oct 18 '22 16:10

daroczig