Je voudrais envelopper un long texte dans ma table kable. Voici un exemple simple de tableau dont le texte de colonne est trop long et doit être encapsulé pour que le tableau tienne sur la page.
---
title: "test"
output: pdf_document
---
```{r setup, include=FALSE}
library(knitr)
```
This is my test
```{r test, echo=FALSE}
test <- data.frame(v1=c("This is a long string. This is a long string. This is a long string. This is a long string. This is a long string.",
"This is a another long string. This is a another long string. This is a another long string. This is a another long string. This is a another long string."),
v2=c(1, 2))
kable(test)
```
J'ai créé le package pander
pour produire des tables de démarque de manière flexible. Par défaut, il fractionnera les cellules avec une chaîne longue à 30 caractères, mais il y a un tas d'arguments options globales et fn pour remplacer cela, activer la césure et d'autres réglages. Démo rapide:
> pander::pander(test)
-----------------------------------
v1 v2
------------------------------ ----
This is a long string. This is 1
a long string. This is a long
string. This is a long string.
This is a long string.
This is a another long string. 2
This is a another long string.
This is a another long string.
This is a another long string.
This is a another long string.
-----------------------------------
> pander::pander(test, split.cell = 80, split.table = Inf)
------------------------------------------------------------------------------------
v1 v2
------------------------------------------------------------------------------- ----
This is a long string. This is a long string. This is a long string. This is a 1
long string. This is a long string.
This is a another long string. This is a another long string. This is a another 2
long string. This is a another long string. This is a another long string.
------------------------------------------------------------------------------------
Une autre solution que le package génial pander
consiste à utiliser column_spec
dans kableExtra
. Dans ce cas, le code suivant fera l'affaire.
kable(test, "latex") %>%
column_spec(1, width = "10em")