最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dataframe - Wrap text for collapse rows in KableExtra for a long table in R - Stack Overflow

programmeradmin2浏览0评论

I have a Rmarkdown file with Yaml section :

---
geometry: top=2cm , bottom= 2.5cm , left=0.5cm, right=0.5cm
output:
  pdf_document:
    latex_engine: xelatex
---

setup r chunk :

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
suppressMessages(library(kableExtra))
suppressMessages(library(tidyverse))

I have a chunk that formats the long table with KableExtra using collapse rows in column 1. The problem is that I want to wrap it and fit in this width of column 1.

DF%>%
  kbl(align = "llccc")%>%
  collapse_rows(columns = 1, valign = "top")%>%
  kable_styling(bootstrap_options = c("bordered"))%>%
  column_spec(1, border_left = TRUE, width = "2cm") %>%
  column_spec(2, width = "12cm")%>%
  column_spec(3, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  column_spec(4, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  column_spec(5,border_right = TRUE, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  row_spec(0, background = "#D3D3D3", bold = TRUE) %>%  
  kable_styling(font_size = 9)

How can I achieve this in R using KableExtra ?

Data

DF = structure(list(category = c("Capability", "Capability", "Capability", 
                                 "Capability", "Capability", "Capability", "Capability", "Capability", 
                                 "Capability", "Capability", "Capability", "Capability", "Capability", 
                                 "Capability", "Challenge", "Challenge", "Challenge", "Challenge", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Core Beliefs", "Core Beliefs", "Expense", "Safety", "Safety", 
                                 "Safety", "Safety", "Safety", "Safety", "Excellence", "Excellence", 
                                 "Excellence"), questions = c("Do you believe that the following attributes define XYZ? Dedicated", 
                                                              "Do you believe that the following attributes define XYZ? Credible", 
                                                              "Do you believe that the following attributes define XYZ? Creative", 
                                                              "Do you believe that the following attributes define XYZ? Sincere", 
                                                              "Do you believe that the following attributes define XYZ? Ambitious", 
                                                              "Do you believe that the following attributes define XYZ? Qualified", 
                                                              "Do you believe that the following attributes define XYZ? Honest", 
                                                              "Extensive Building knowledge", "The capacity to rapidly share data on your projects", 
                                                              "The skill to coordinate your projects", "Skilled and talented personnel", 
                                                              "A variety of building services that can be adjusted to fit your demands", 
                                                              "Do you believe that the following attributes define XYZ? Dedicated", 
                                                              "Do you believe that the following attributes define XYZ? Credible", 
                                                              "Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?", 
                                                              "Kindly respond to the following inquiries: How inclined are you to use our solutions again?", 
                                                              "Kindly respond to the following inquiries: Would you refer XYZ to associates?", 
                                                              "Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?", 
                                                              "How pleased are you with the pace at which XYZ handled your concerns and grievances?", 
                                                              "The final resolution of your concerns and grievances", "Please evaluate your contentment regarding the following: Billing", 
                                                              "Please evaluate your contentment regarding the following: Requests", 
                                                              "Please evaluate your contentment regarding the following: Adherence to your needs", 
                                                              "Please evaluate your contentment regarding the following: Prompt reaction to your needs", 
                                                              "Please evaluate your contentment regarding the following: Compliance with the plan", 
                                                              "How pleased are you with XYZ executives’ accessibility?", "How pleased are you with XYZ’s efficiency in field monitoring?", 
                                                              "How pleased are you with XYZ’s capacity to accomplish tasks as requested?", 
                                                              "How pleased are you with team expertise and comprehension of building techniques?", 
                                                              "How pleased are you with issue resolution (Provide corresponding examples)?", 
                                                              "How pleased are you with cooperation with client teams?", 
                                                              "How pleased are you with XYZ executives’ accessibility?", "XYZ is devoted to offering exceptional quality service; how pleased are you with our outputs?", 
                                                              "How pleased are you with XYZ policies concerning Security, Protection, and Well-being?", 
                                                              "Please evaluate your contentment regarding the following: The entire expenditure of the undertaking (Original proposal & requests)", 
                                                              "How pleased are you with XYZ policies concerning Security, Protection, and Well-being?", 
                                                              "XYZ Security Framework on the Project", "XYZ’s accountability and dedication to Security on the Project", 
                                                              "Proficiency of XYZ Project Security Team", "Clarity, efficiency, and openness of reports linked to incident assessments", 
                                                              "Dialogue between XYZ project executives and yours concerning Security", 
                                                              "XYZ’s accountability and dedication to Excellence on the project", 
                                                              "XYZ’s Excellence Administration Framework execution on the project", 
                                                              "Productivity, proficiency, and openness concerning remedial steps on quality aspects"
                                 ), `House ball Score (/5)` = c(3, 3, 3, 2, 3, 4, 3, 3, 3, 
                                                                     2, 2, 3, 2, 2, 3, 3, 3, 2, 3, 3, 3, 3, 2, 2, 1, 3, 2, 2, 3, 2, 
                                                                     2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), `House toy Score (/5)` = c(2L, 
                                                                                                                                                3L, 3L, 1L, 5L, 2L, 3L, 3L, 2L, 5L, 4L, 1L, 4L, 5L, 1L, 2L, 3L, 
                                                                                                                                                5L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 3L, 2L, 2L, 1L, 1L, 3L, 4L, 
                                                                                                                                                5L, 4L, 5L, 3L, 4L, 4L, 4L, 2L, 3L, 5L, 5L), `House Car Score (/5)` = c(4L, 
                                                                                                                                                                                                                         1L, 1L, 2L, 5L, 2L, 1L, 3L, 3L, 4L, 3L, 4L, 4L, 1L, 1L, 5L, 4L, 
                                                                                                                                                                                                                         1L, 1L, 4L, 5L, 4L, 4L, 2L, 5L, 2L, 1L, 3L, 5L, 4L, 2L, 3L, 4L, 
                                                                                                                                                                                                                         2L, 2L, 1L, 3L, 1L, 4L, 2L, 5L, 3L, 3L, 3L)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                     -44L), class = c("tbl_df", "tbl", "data.frame"))

I have a Rmarkdown file with Yaml section :

---
geometry: top=2cm , bottom= 2.5cm , left=0.5cm, right=0.5cm
output:
  pdf_document:
    latex_engine: xelatex
---

setup r chunk :

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
suppressMessages(library(kableExtra))
suppressMessages(library(tidyverse))

I have a chunk that formats the long table with KableExtra using collapse rows in column 1. The problem is that I want to wrap it and fit in this width of column 1.

DF%>%
  kbl(align = "llccc")%>%
  collapse_rows(columns = 1, valign = "top")%>%
  kable_styling(bootstrap_options = c("bordered"))%>%
  column_spec(1, border_left = TRUE, width = "2cm") %>%
  column_spec(2, width = "12cm")%>%
  column_spec(3, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  column_spec(4, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  column_spec(5,border_right = TRUE, width = "1.5cm", extra_css = "vertical-align: middle;")%>%
  row_spec(0, background = "#D3D3D3", bold = TRUE) %>%  
  kable_styling(font_size = 9)

How can I achieve this in R using KableExtra ?

Data

DF = structure(list(category = c("Capability", "Capability", "Capability", 
                                 "Capability", "Capability", "Capability", "Capability", "Capability", 
                                 "Capability", "Capability", "Capability", "Capability", "Capability", 
                                 "Capability", "Challenge", "Challenge", "Challenge", "Challenge", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Contracting Management Topics", 
                                 "Contracting Management Topics", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Cooperation Site Supervision", "Cooperation Site Supervision", 
                                 "Core Beliefs", "Core Beliefs", "Expense", "Safety", "Safety", 
                                 "Safety", "Safety", "Safety", "Safety", "Excellence", "Excellence", 
                                 "Excellence"), questions = c("Do you believe that the following attributes define XYZ? Dedicated", 
                                                              "Do you believe that the following attributes define XYZ? Credible", 
                                                              "Do you believe that the following attributes define XYZ? Creative", 
                                                              "Do you believe that the following attributes define XYZ? Sincere", 
                                                              "Do you believe that the following attributes define XYZ? Ambitious", 
                                                              "Do you believe that the following attributes define XYZ? Qualified", 
                                                              "Do you believe that the following attributes define XYZ? Honest", 
                                                              "Extensive Building knowledge", "The capacity to rapidly share data on your projects", 
                                                              "The skill to coordinate your projects", "Skilled and talented personnel", 
                                                              "A variety of building services that can be adjusted to fit your demands", 
                                                              "Do you believe that the following attributes define XYZ? Dedicated", 
                                                              "Do you believe that the following attributes define XYZ? Credible", 
                                                              "Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?", 
                                                              "Kindly respond to the following inquiries: How inclined are you to use our solutions again?", 
                                                              "Kindly respond to the following inquiries: Would you refer XYZ to associates?", 
                                                              "Please evaluate your contentment regarding the following: Compared to competitors, how pleased are you with XYZ?", 
                                                              "How pleased are you with the pace at which XYZ handled your concerns and grievances?", 
                                                              "The final resolution of your concerns and grievances", "Please evaluate your contentment regarding the following: Billing", 
                                                              "Please evaluate your contentment regarding the following: Requests", 
                                                              "Please evaluate your contentment regarding the following: Adherence to your needs", 
                                                              "Please evaluate your contentment regarding the following: Prompt reaction to your needs", 
                                                              "Please evaluate your contentment regarding the following: Compliance with the plan", 
                                                              "How pleased are you with XYZ executives’ accessibility?", "How pleased are you with XYZ’s efficiency in field monitoring?", 
                                                              "How pleased are you with XYZ’s capacity to accomplish tasks as requested?", 
                                                              "How pleased are you with team expertise and comprehension of building techniques?", 
                                                              "How pleased are you with issue resolution (Provide corresponding examples)?", 
                                                              "How pleased are you with cooperation with client teams?", 
                                                              "How pleased are you with XYZ executives’ accessibility?", "XYZ is devoted to offering exceptional quality service; how pleased are you with our outputs?", 
                                                              "How pleased are you with XYZ policies concerning Security, Protection, and Well-being?", 
                                                              "Please evaluate your contentment regarding the following: The entire expenditure of the undertaking (Original proposal & requests)", 
                                                              "How pleased are you with XYZ policies concerning Security, Protection, and Well-being?", 
                                                              "XYZ Security Framework on the Project", "XYZ’s accountability and dedication to Security on the Project", 
                                                              "Proficiency of XYZ Project Security Team", "Clarity, efficiency, and openness of reports linked to incident assessments", 
                                                              "Dialogue between XYZ project executives and yours concerning Security", 
                                                              "XYZ’s accountability and dedication to Excellence on the project", 
                                                              "XYZ’s Excellence Administration Framework execution on the project", 
                                                              "Productivity, proficiency, and openness concerning remedial steps on quality aspects"
                                 ), `House ball Score (/5)` = c(3, 3, 3, 2, 3, 4, 3, 3, 3, 
                                                                     2, 2, 3, 2, 2, 3, 3, 3, 2, 3, 3, 3, 3, 2, 2, 1, 3, 2, 2, 3, 2, 
                                                                     2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), `House toy Score (/5)` = c(2L, 
                                                                                                                                                3L, 3L, 1L, 5L, 2L, 3L, 3L, 2L, 5L, 4L, 1L, 4L, 5L, 1L, 2L, 3L, 
                                                                                                                                                5L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 1L, 3L, 2L, 2L, 1L, 1L, 3L, 4L, 
                                                                                                                                                5L, 4L, 5L, 3L, 4L, 4L, 4L, 2L, 3L, 5L, 5L), `House Car Score (/5)` = c(4L, 
                                                                                                                                                                                                                         1L, 1L, 2L, 5L, 2L, 1L, 3L, 3L, 4L, 3L, 4L, 4L, 1L, 1L, 5L, 4L, 
                                                                                                                                                                                                                         1L, 1L, 4L, 5L, 4L, 4L, 2L, 5L, 2L, 1L, 3L, 5L, 4L, 2L, 3L, 4L, 
                                                                                                                                                                                                                         2L, 2L, 1L, 3L, 1L, 4L, 2L, 5L, 3L, 3L, 3L)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                     -44L), class = c("tbl_df", "tbl", "data.frame"))

Share Improve this question asked Mar 8 at 11:36 Homer Jay SimpsonHomer Jay Simpson 1,2688 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

To quote the warning

Usually it is recommended to use column_spec before collapse_rows, especially in LaTeX, to get a desired result

we can place column_spec(1,..) before collapse_rows

DF %>%
  kable(align = "llccc", format = "latex") %>%
  kable_styling(font_size = 9, bootstrap_options = c("bordered")) %>%
  column_spec(1, width = "2cm", border_left = TRUE) %>%
  column_spec(2, width = "12cm") %>%
  column_spec(3:4, width = "1.5cm") %>%
  column_spec(5, width = "1.5cm", border_right = TRUE) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  row_spec(0, background = "#D3D3D3", bold = TRUE) # the background overwrites vertical header lines and I could not fix this

output

发布评论

评论列表(0)

  1. 暂无评论