I would like to split the column headers of a table. I've already looked at this note https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html, but I haven't found answers to my issue. I've tried to use the column_spec but didn't work. Also, I would like to highlight the column headers through a color.
Here below a example:
---
title: "Report"
output: html_document
---
```{r}
library(knitr)
library(kableExtra)
```
```{r}
options(knitr.table.format = "html")
```
```{r}
text_tbl <- data.frame(
Items = c("Item 1", "Item 2", "Item 3"),
Features = c(
"Description 1",
"Description 2",
"Description 3"
)
)
names(text_tbl)[2]="Column name to break"
kable(text_tbl, "html") %>%
kable_styling(full_width = F) %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, width = "30em")
```
Thank you all
Try to decrease the width for column 2 a little and use row_spec(0, ...) for the header row.
kable(text_tbl, "html") %>%
kable_styling(full_width = F) %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, width = "8em") %>%
row_spec(0, color = "red")
Related
My little example is as follows:
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
library(flextable)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
library(magrittr) ### for %>%
ft = head(mtcars[,1:5]) %>% flextable() %>%
set_caption(caption = "display a subset of columns for mtcars data") %>%
# align(part = "caption", align = "center") %>%
# font(part = "caption", fontname = "Calibri") %>%
# italic(italic = FALSE, part = "caption") %>%
add_footer_lines("also can be based on optional argument col_keys") %>%
font(part = "footer", fontname = "Consolas") %>%
bold(bold = TRUE, part = "footer")
ft
```
The Word Rmarkdown output shows the caption with left-alignment, Cambria-font and italic. I want to customize the caption part for the table with center-alignment, some-other-font and no-italic. However, the flextable package has no part for caption, with partname of the table (one of ’all’, ’body’, ’header’, ’footer’). How can I achieve the goal?
This is only available in the development version for now:
New features have been added recently:
paragraph settings
ability to define a complex paragraph with different formats for chunks
---
output:
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(flextable)
library(officer)
set_flextable_defaults(
font.family = "Arial",
font.size = 9,
big.mark = "",
theme_fun = "theme_vanilla"
)
```
```{r}
ft = head(mtcars[,1:5]) |> flextable() |>
set_caption(
caption = as_paragraph(
as_chunk("display a subset of columns",
props = fp_text_default(font.family = "Courier", bold = FALSE)),
as_chunk(" for mtcars data",
props = fp_text_default(color = "red", font.family = "Helvetica", bold = TRUE))
),
fp_p = fp_par(text.align = "center", padding = 5)
) |>
add_footer_lines("blah blah blah") |>
font(part = "footer", fontname = "Consolas") |>
bold(bold = TRUE, part = "footer")
ft
```
to install the current dev version:
remotes::install_github("davidgohel/officer")
remotes::install_github("davidgohel/flextable")
I'm trying to remove these gaps in my kable table (latex / pdf / rmarkdown):
I essentially just want an outside border and a line that separates the two columns. I thought the best way of doing this was with border_left and border_right, but maybe I'm wrong there?
Code:
table <- tribble(~col1, ~col2,
"Data:", "Some text here.",
"Transformations:", str_c("Other text here."))
table %>%
kable("latex", booktabs = T, col.names = NULL, align = "ll") %>%
kable_styling(full_width = T, latex_options = "hold_position") %>%
column_spec(1, bold = T, border_left = T, border_right = T, width = "2.8cm") %>%
column_spec(2, border_right = T)
Any help would be appreciated!
How you can solve this problem?
Simply make it into console.
library(kableExtra)
library(tidyverse)
table <- tribble(~col1, ~col2,
"Data:", "Some text here.",
"Transformations:", str_c("Other text here."))
table %>%
kable("latex",col.names = NULL) %>%
kable_paper(latex_options = "hold_position") %>%
column_spec(1, bold = T, border_left = T) %>%
column_spec(2, border_right = T)
After you'll receive this output:
\begin{table}[!h]
\centering
\begin{tabular}{|>{\raggedright\arraybackslash}p{2.8cm}|>{}l|}
\hline
Data: & Some text here.\\
\hline #remove this line
\textbf{Transformations:} & Other text here.\\
\hline
\end{tabular}
\end{table}
Copy and paste this into your RMarkdown and... Bingo:
One approach:
library(huxtable)
tribble(~col1, ~col2,
"Data:", "Some text here.",
"Transformations:", "Other text here."
)|>
as_huxtable(add_colnames = FALSE) |>
set_lr_borders() |>
set_outer_borders()
Well I have a table in Rmarkdown but since I write the document in Greek I want also the Table word to be printed in Greek
```{r name, echo=FALSE,results='asis'}
kablegr %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
what I want is instead o Table to print Πίνακας in each table of my document
Table: --> Πίνακας:
OPTION 1
This a solution that keeps the numbering automatically (following this answer. Nonetheless, I had to suppress the bold face.
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output: html_document
---
```{r, include=F}
library(captioner)
tables <- captioner(prefix = "Πίνακας")
```
```{r tab1, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab1", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
```{r tab2, echo=FALSE}
library(kableExtra)
mtcars %>%
kbl(caption=tables("tab2", "Table Caption")) %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output
OPTION 2
You could try this (fig_caption: no in your YAML header):
---
title: "Untitled"
author: "bttomio"
date: "5/10/2021"
output:
html_document:
fig_caption: no
---
```{r name, echo=FALSE,results='asis'}
library(kableExtra)
mtcars %>%
kbl(caption = "**_Πίνακας_**") %>%
kable_classic(full_width = F, html_font = "Cambria")%>%
column_spec(1, bold = TRUE, border_right = TRUE, color = "black", background = "lightgrey")
```
-output
Please see the rmd file below. I'd like to angle some column headers and have widths be respected.
In the first table without row_spec() the widths are respected but when row_spec() is added to angle a few column headers the widths no longer hold. Any idea how to angle and have the widths remain?
rmd file
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
```{r table1, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
dt <- mtcars[1:5, 1:6]
rownames(dt) = NULL
colnames(dt)= c("long col 1","long col 12","long col 13","long col 14","long col 15","long col 16")
kable(
dt,
format ="latex",
caption = "No angle",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```
```{r table2, echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE,results='asis'}
kable(
dt,
format ="latex",
caption = "With Angled columns",
booktabs = T,
longtable = T,
escape = F,
align = "c",
linesep=""
) %>%
kable_styling(latex_options = c("repeat_header" ), font_size=8)%>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
row_spec(0, angle = c(rep(0,2),rep(90,2),rep(0,2) ) )%>%
column_spec(c(1,2,3,4,5,6), width = c("8em","5em","6em","3em","3em","3em" ) )
```
I've having some issues with the kableExtra output in RStudio. I'm trying to create a formatted table in an R markdown file once I hit the 'knit' button.
The code is:
kable(temp_table, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 11, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
It should create a simple table with a header row that I added. But not only does that not show up, but neither do the variable row names. And what precedes the table is a bunch of HTML code that looks like it wasn't rendered.
Some version history:
kableExtra - 0.6.1
knitr - 1.17
rmarkdown - 1.7
R - 3.3.3
RStudio - 1.1.258
Would appreciate any help with this!
---- UPDATE ----
Below is a simple reproducible example that gives me the same kind of output.
---
title: "Iris Example"
output_file: "iris2.html"
---
## Iris Data
``` {r iris, echo = FALSE, warning = FALSE}
library(data.table)
library(knitr)
library(kableExtra)
iris <- data.table(iris)
iris <- iris[, .(
sep_len = mean(Sepal.Length)
, sep_wid = mean(Sepal.Width)
, pet_len = mean(Petal.Length)
, pet_wid = mean(Petal.Width)
), by = .(Species)]
kable(iris, "html") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2)) %>%
kable_styling(bootstrap_options = c("striped", "condensed", "hover", "responsive"), font_size = 10, full_width = FALSE, position = "left") %>%
column_spec(1, bold = T)
```
I've reproduced this output only on an OS X.
Installing the latest version of kableExtra (0.7.0.9000) with
devtools::install_github("haozhu233/kableExtra") solved the problem for me.