Подтвердить что ты не робот

Обтекание длинного текста в столбце таблицы kable

Я хотел бы обернуть длинный текст в моей таблице kable. Вот простой пример таблицы с слишком длинным текстом столбца, который должен быть обернут для таблицы, чтобы она поместилась на странице.

---
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)
```

enter image description here

4b9b3361

Ответ 1

Я создал пакет pander для создания таблиц разметки гибким способом. По умолчанию он будет разделять ячейки с длинной строкой на 30 символов, но есть куча глобальных параметров и аргументов fn, чтобы переопределить это, включить перенос и другие трюки. Быстрая демонстрация:

> 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.        
------------------------------------------------------------------------------------

Ответ 2

Альтернативным решением, отличным от удивительного пакета pander, является использование column_spec в kableExtra. В этом случае следующий код будет делать трюк.

kable(test, "latex") %>%
  column_spec(1, width = "10em")