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.
Related
I want to add a header to grouped data in my data.frame structured dataset. please, see the input and output. Input;`
data<-data.frame(A=c(1,2,3,4), B=c(7,8,9,10), C=c(11,12,13,14), D=c(15,16,17,18))
Output
If you want this for display purposes there are few packages which can help you.
For example, with knitr and kableExtra.
library(knitr)
library(kableExtra)
kable(data) %>%
kable_styling(
full_width = FALSE,
bootstrap_options = c("striped", "hover", "condensed"),
) %>%
add_header_above(c(Group1 = 2, Group2 = 2))
I'm trying to add footnote under my table but foot note is not getting displayed.
kbl(dataf,format="latex",longtable = T) %>%
kable_styling(latex_options = c("hold_position","repeat_header"),bootstrap_options = "bordered",full_width = F)%>%
row_spec(0, bold = T)%>%
add_footnote("klvnkkkkkkkkkkkkkkkkkkkkkkkkkkkkk",notation="number")%>%pack_rows("Group 1", 4, 5)
I am currently working on a Markdown file (with latex) where I use kable() and kableExtra for my tables. The problem is that some of my tables are to big and doesn't fit on a pdf page (even in landscape).
I have tried to use latex_options = "scale_down" from kableExtra but for some reasons it doesn't work, it doesn't change anything. Here is an example of the code I'm running :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3)) %>%
kable_styling(latex_options = c("striped", "scale_down", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "10cm")
I already looked on Google and stackoverflow. Anyone have an idea of what I'm doing wrong? Thanks
Edit, here is the working code as requested in comments :
kable(dt, "latex", longtable = T, caption = "SampleCaption") %>%
add_header_above(c("","Mens" = 3, "Womens" = 3, "Total" = 2)) %>%
kable_styling(font_size = 7, latex_options = c("striped", "repeat_header"),repeat_header_text = "",
full_width = F) %>%
column_spec(1, width = "5cm")
This is not an answer but more like a clarification that since scale_down is using the resizebox in package graphicx while longtable is longtable and these two latex packages won't talk with each other, scale_down only works for normal tables.
In fact, you should be seeing a note in your console that "scale_down" doesn't work with longtable
Source in kableExtra
if (table_info$tabular == "longtable") {
warning("Longtable cannot be resized.")
return(x)
}
I'm trying to create a table in Rmarkdown (for pdf) based on the diamonds data set and I want to change some column names and headers above the column names, I would like to know how I can set some headers in italic or bold and some headers should be a symbol (like the one for partial eta squared) or a complete formula. I've included my R code (and I have installed Latex) with the table as a picture (not adjusted yet).
```{r setup, include=FALSE}
setwd("~/Desktop/Tables")
knitr::opts_chunk$set(echo = FALSE)
# global options
options(knitr.table.format = "latex")
# show space instead of NA in tables
options(knitr.kable.NA = '')
library(tidyverse)
library(knitr)
library(kableExtra)
df = diamonds
```
```{r message=FALSE, warning=FALSE}
df_table = df %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = "Total") %>%
select(grouping, avg, sd, n, range) %>%
bind_rows(df %>%
group_by(cut) %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = as.character(cut)) %>%
select(grouping, avg, sd, n, range))
kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "M in italic", "SD in italic", "N not
italic", "some weird formula for the range" )) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "the formula for the variance" = 4))
```
Set escape = FALSE in your call to kable and add_header_above and use LaTeX in your column headings. I should admit that it's a mystery to me exactly how many backslashes should be added to get the desired result.
kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "$M$", "$SD$", "N", "$\\eta$"), escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "$\\\\operatorname{Var}[X]$" = 4), escape = FALSE)
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")