Обернуть текст заголовка столбца, column_spe c не работает для kable_styling - PullRequest
0 голосов
/ 19 марта 2020

У меня есть таблица, которая печатается. Я указал ширину столбца в функции column_spec, однако заголовки моих столбцов не переносятся в соответствии с указанной шириной.

Код выглядит следующим образом;

 kable(ranknew, format = "latex",  row.names = FALSE, 
     align = c("c", "l", "r", "r", "r"), 
     caption = paste("We had", nrow(ranknew) , 
     "brokerages give us 100+ new policies in", year(Sys.Date())-1), 
     longtable = TRUE, booktabs = TRUE) %>%  
 kable_styling(latex_options = c("HOLD_position", "repeat_header"), full_width = F, font_size = 9, position = "center")  %>% 
 column_spec(column = 1, width = "3cm")%>% 
 column_spec(column = 2, width = "8cm") %>% 
 column_spec(column = 3, width = "2cm") %>% 
 column_spec(column = 4, width = "1.5cm") %>% 
 column_spec(column = 5, width = "1.5cm") %>% 
 row_spec(row = 0, bold = TRUE, underline = TRUE, background = "orange", 
          color = "black", italic = FALSE, monospace = FALSE, strikeout = FALSE) %>% 
 print(floating = FALSEFALSE, row.names = FALSE)

Это то, что генерирует мой код:

enter image description here

Очевидно, я хотел бы перенести заголовки столбцов. Порядок column_spec и row_spec не имеет значения.

1 Ответ

1 голос
/ 20 марта 2020

Добавьте col.names () к функции kable () в начале и удалите подчеркивание = TRUE из функции row_spe c ().

kable(ranknew, format = "latex",  row.names = FALSE, 
   col.names = c("Provinces","Brokerage Name","Written Pren....","Loss Ratio","New Business( Whatever comes here)"),
     align = c("c", "l", "r", "r", "r"), 
     caption = paste("We had", nrow(ranknew) , 
     "brokerages give us 100+ new policies in", year(Sys.Date())-1), 
     longtable = TRUE, booktabs = TRUE) %>%  
 kable_styling(latex_options = c("HOLD_position", "repeat_header"), full_width = F, font_size = 9, position = "center")  %>% 
 column_spec(column = 1, width = "3cm")%>% 
 column_spec(column = 2, width = "8cm") %>% 
 column_spec(column = 3, width = "2cm") %>% 
 column_spec(column = 4, width = "1.5cm") %>% 
 column_spec(column = 5, width = "1.5cm") %>% 
 row_spec(row = 0, bold = TRUE,  background = "orange", 
          color = "black", italic = FALSE, monospace = FALSE, strikeout = FALSE) %>% 
 print(floating = FALSEFALSE, row.names = FALSE)

Этот пример работает на моем конце с набор данных iris.

library(knitr)
library(kableExtra)
library(tidyverse)

 kable(iris, format = "latex",  row.names = FALSE, 
       col.names = c("Provinces","Brokerage Name","Written Pren....","Loss Ratio","New Business( Whatever comes here)"),
     longtable = TRUE, booktabs = TRUE) %>%  
 kable_styling(latex_options = c("HOLD_position", "repeat_header"), full_width = F, font_size = 9, position = "center")  %>% 
 column_spec(column = 1, width = "3cm")%>% 
 column_spec(column = 2, width = "8cm") %>% 
 column_spec(column = 3, width = "2cm") %>% 
 column_spec(column = 4, width = "1.5cm") %>% 
 column_spec(column = 5, width = "1.5cm") %>% 
 row_spec(row = 0, bold = TRUE, underline = TRUE, background = "orange", 
          color = "black", italic = FALSE, monospace = FALSE, strikeout = FALSE) %>% 
 print(floating = FALSEFALSE, row.names = FALSE)
...