admin管理员组文章数量:1386677
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. Before i set the data frame to kbl()
i conditional color the columns 3:5 .The problem is that the borders do not show in the conditional formatted cells (verically and horizontally). How can i show the borders without ruining the formating? I tried booktabs =TRUE
but this creates empty white space between the row lines Also the values in columns 3:5 to be justify and not at the top .
{r, warning = FALSE ,message=FALSE,fig.width=9, fig.height=9, dpi= 1200}
format_column <- function(x) {
case_when(
x < 1.5 ~ cell_spec(x, background = "#ed2e1c", color = "black",format = "latex" ), # [1–1.5[
x >= 1.5 & x <= 2.5 ~ cell_spec(x, background = "#e09c95", color = "black",format = "latex"), # [1.5–2.5]
x > 2.5 & x < 3.5 ~ cell_spec(x, background = "#85c1e9", color = "black", format = "latex"), # ]2.5–3.5[
x >= 3.5 & x <= 4.5 ~ cell_spec(x, background = "#7FF98B", color = "black", format = "latex"), # [3.5–4.5]
x > 4.5 & x <= 5 ~ cell_spec(x, background = "#04B431", color = "black", format = "latex"), # ]4.5–5]
TRUE ~ cell_spec(x, background = "white" , color = "black", format = "latex") # Default
)
}
{r, warning = FALSE ,message=FALSE,fig.width=9, fig.height=9, dpi= 1200,results='asis'}
df_formatted = DF %>%
mutate(across(c(3:5), format_column))
df2 = df_formatted %>%
kable(align = "llccc", format = "latex", escape = FALSE) %>%
kable_styling(font_size = 7, bootstrap_options = c("bordered"), latex_options = "HOLD_position") %>%
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 = "middle") %>%
row_spec(0, background = "#D3D3D3", bold = TRUE)
print(df2)
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. Before i set the data frame to kbl()
i conditional color the columns 3:5 .The problem is that the borders do not show in the conditional formatted cells (verically and horizontally). How can i show the borders without ruining the formating? I tried booktabs =TRUE
but this creates empty white space between the row lines Also the values in columns 3:5 to be justify and not at the top .
{r, warning = FALSE ,message=FALSE,fig.width=9, fig.height=9, dpi= 1200}
format_column <- function(x) {
case_when(
x < 1.5 ~ cell_spec(x, background = "#ed2e1c", color = "black",format = "latex" ), # [1–1.5[
x >= 1.5 & x <= 2.5 ~ cell_spec(x, background = "#e09c95", color = "black",format = "latex"), # [1.5–2.5]
x > 2.5 & x < 3.5 ~ cell_spec(x, background = "#85c1e9", color = "black", format = "latex"), # ]2.5–3.5[
x >= 3.5 & x <= 4.5 ~ cell_spec(x, background = "#7FF98B", color = "black", format = "latex"), # [3.5–4.5]
x > 4.5 & x <= 5 ~ cell_spec(x, background = "#04B431", color = "black", format = "latex"), # ]4.5–5]
TRUE ~ cell_spec(x, background = "white" , color = "black", format = "latex") # Default
)
}
{r, warning = FALSE ,message=FALSE,fig.width=9, fig.height=9, dpi= 1200,results='asis'}
df_formatted = DF %>%
mutate(across(c(3:5), format_column))
df2 = df_formatted %>%
kable(align = "llccc", format = "latex", escape = FALSE) %>%
kable_styling(font_size = 7, bootstrap_options = c("bordered"), latex_options = "HOLD_position") %>%
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 = "middle") %>%
row_spec(0, background = "#D3D3D3", bold = TRUE)
print(df2)
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
edited Mar 18 at 14:00
Homer Jay Simpson
asked Mar 18 at 13:34
Homer Jay SimpsonHomer Jay Simpson
1,2468 silver badges36 bronze badges
7
- You wouldn't have this problem if pandoc supported tabularray tables as suggested in github/jgm/pandoc/issues/7475 – samcarter_is_at_topanswers.xyz Commented Mar 18 at 13:54
- @samcarter_is_at_topanswers.xyz so it cannot be done ? – Homer Jay Simpson Commented Mar 18 at 13:57
- @TimG i fot to mention it.It is "results = 'asis'" option in the chunk. I remember you discussing it.I include it but still the borders do not show. – Homer Jay Simpson Commented Mar 18 at 14:00
- No, I'm saying that pandoc should support tabularray to avoid such problems. – samcarter_is_at_topanswers.xyz Commented Mar 18 at 14:00
- @samcarter_is_at_topanswers.xyz so how i can be done ? – Homer Jay Simpson Commented Mar 18 at 14:02
1 Answer
Reset to default 1I found a solution using the hhline
package instead of cline
---
geometry: top=2cm , bottom= 2.5cm , left=0.5cm, right=0.5cm
output:
pdf_document:
latex_engine: xelatex
header-includes: |
\usepackage{hhline}
---
```{r setup, include=FALSE, echo=F, warning=F}
knitr::opts_chunk$set(echo = TRUE)
suppressMessages(library(kableExtra))
suppressMessages(library(tidyverse))
```
```{r c, echo=F, results='asis'}
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"))
# Apply cell_spec to columns 3 to 5
DF <- DF %>%
mutate(across(3:5, ~ case_when(
.x < 1.5 ~ paste0("\\cellcolor[HTML]{ed2e1c}",.x), # [1–1.5[
.x >= 1.5 & .x <= 2.5 ~ paste0("\\cellcolor[HTML]{e09c95}",.x), # [1.5–2.5]
.x > 2.5 & .x < 3.5 ~ paste0("\\cellcolor[HTML]{85c1e9}",.x), # ]2.5–3.5[
.x >= 3.5 & .x <= 4.5 ~ paste0("\\cellcolor[HTML]{7FF98B}",.x), # [3.5–4.5]
.x > 4.5 & .x <= 5 ~ paste0("\\cellcolor[HTML]{04B431}",.x), # ]4.5–5]
TRUE ~ paste0("\\cellcolor{white}",.x) # Default
)))
# Generate the kable table with styling
df2 <- DF %>%
kbl(format = "latex", align = "llccc") %>%
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 = "middle") %>%
row_spec(0, background = "#D3D3D3", bold = TRUE)
df2 <- gsub("\\\\\\{", "{", df2)
df2 <- gsub("\\\\\\}", "}", df2)
df2 <- gsub("cellcolor[HTML]{ed2e1c}", "\\cellcolor[HTML]{ed2e1c}", df2, fixed = TRUE)
df2 <- gsub("cellcolor[HTML]{e09c95}", "\\cellcolor[HTML]{e09c95}", df2, fixed = TRUE)
df2 <- gsub("cellcolor[HTML]{85c1e9}", "\\cellcolor[HTML]{85c1e9}", df2, fixed = TRUE)
df2 <- gsub("cellcolor[HTML]{7FF98B}", "\\cellcolor[HTML]{7FF98B}", df2, fixed = TRUE)
df2 <- gsub("cellcolor[HTML]{04B431}", "\\cellcolor[HTML]{04B431}", df2, fixed = TRUE)
df2 <- gsub("\\textbackslash{}", "", df2, fixed = TRUE)
# Replace \cline with hhline
df2 <- gsub("\\cline{2-5}", "\\hhline{~----}", df2, fixed = TRUE)
df2 <- gsub("\\cline{1-5}", "\\hhline{-----}", df2, fixed = TRUE)
cat(df2)
```
giving
本文标签: rBorders do not display in a conditional formatted KableExtra longtableStack Overflow
版权声明:本文标题:r - Borders do not display in a conditional formatted KableExtra longtable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744510794a2609869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论