RMarkdown Kable Bandwidth Issues

I am trying to create a table to store some basic information using Kable in RMarkdown, which 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 the way the table fills html. However, when I create the PDF, I get the following. enter image description here

As we can see, there are problems with the PDF, the first table goes from the page, and the second does not fill the entire width. I unfortunately have n00b when it comes to R, Kable and RMarkdown. Is there a way to set parameters in the kable table so that the PDF looks as good as HTML in terms of page layout and width? Thanks!

+5
source share
1 answer

LaTeX will not break long lines inside a table cell for you - or you should use a custom environment for this, which can sometimes be very unpleasant when writing markdowns that should be handled by Pandoc. That's why I came up with the idea of ​​breaking long lines inside cells before moving on to markdown with 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 have multiple 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, find the global parameters table.split.table and table.split.cells although there are other useful settings.

+4
source

Source: https://habr.com/ru/post/1216133/


All Articles