---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r mtcars, warning=FALSE, message=FALSE, echo=FALSE, results='asis'}
library(tidyverse)
library(kableExtra)
kable(mtcars %>%
select(1:5) %>%
head(10) %>%
mutate(cyl = cell_spec(cyl, align = "r")),
"html",
escape = FALSE) %>%
kable_styling("striped", "hover", full_width = TRUE) %>%
row_spec(c(1, 3), background = "yellow")
```
I follow the KableExtra guide to align cells using the cell_spec() function. I am trying to right align my cyl column. But notice the column is not right aligned (image below).
What went wrong?
Jason, the kable function itself has an align option. I think if you are not looking for any conditional alignment, you should just use that.
library(kableExtra)
library(tidyverse)
mtcars %>%
select(1:5) %>%
head(10) %>%
kable(align = c("crcccc")) %>%
kable_styling("striped", "hover", full_width = TRUE) %>%
row_spec(c(1, 3), background = "yellow")
For cell_spec itself, it seems like right now this align option for HTML just won't give you what you need. If you check the raw HTML generated by this approach, you will see ultimately, cell_spec puts your cell into a <span> tag. You can specify text_align there but that gets overrided by the text_align setting in <td>. I will see if I should fix this or add a warning note here in this section.
<td style="text-align:left;"> <span style=" text-align: right;">4</span> </td>
Related
I am using knitr::kable to print my dataframes, but sometimes they are too big. Is there any simple way to print them compactly with scrollbar?
For example, I do:
knitr::kable(mtcars)
How could I add scrolling by condition (for example, if nrow > 10 and/or ncol > 10)?
P.S. DT::datatable doesn't work for big ncol:
I need exactly scrolling interface.
You can add scrollbars. For example, with kableExtra or DT:
R Markdown
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
Some very wide data:
```{r}
df <- cbind(mtcars, mtcars)
```
With `kableExtra`:
```{r}
kable(df) %>%
kable_styling("striped", full_width = F) %>%
scroll_box(width = "100%", height = "200px")
```
Or with `DT`:
```{r}
DT::datatable(
df,
height = 200,
options = list(scrollX = TRUE)
)
```
output
The vertical scrollbar generated with kableExtra works in RStudio but doesn't work in the knitted html_document.
Example
Take the following Rmd file
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(dplyr)
library(kableExtra)
```
```{r}
iris %>%
kable %>%
kable_styling("striped", full_width = F)
```
When running the chunk in RStudio, I see a vertical scrollbar
But after knitting, the vertical scrollbar is gone and the whole data.frame is printed
How can I get the vertical scrollbar to display in the knitted HTML the same as it was in RStudio?
Have a look at the function scroll_box() from the package kableExtra.
```{r}
iris %>%
kable %>%
kable_styling("striped", full_width = F) %>%
scroll_box(width = "500px", height = "200px")
```
To specify the size of the box, see the arguments in the documentation
I am trying to create a table using kable/kableextra without showing the horizontal lines in the table except for the first row which is the row names.
```
{r echo=FALSE}
library(knitr)
library(kableExtra)
options(knitr.kable.NA = '')
dt <- mtcars[1:5, 1:6]
kable(dt, "html") %>%
kable_styling(full_width = F, position = "left") %>%
row_spec(0, align = "c",bold=T ) %>%
column_spec(1, bold = T)
```
In the code above there is a line below the first row, which I like since those are row names, but there are lines between every row which I would like to remove.
Ideally I would like to have a slightly thicker line at the top at bottom of this table. Similar to the booktabs look in LaTeX.
I have read the documentation but the CSS is beyond me.
Thanks for any suggestions.
What you need is to set booktabs = T argument inside kable.
In your example, just change the following line of code:
kable(dt, "html")
to:
kable(dt, "html", booktabs = T)
Cheers!
You can include a LaTeX table in your html doc as an image, but you need a complete LaTeX distribution (not tinytex) and the R package magick (+Ghostscript if you are on Windows).
Replace
kable(dt, "html") %>%
with
kable(dt, "latex", booktabs=T) %>%
and add
kable_as_image()
as last line (dont forget the pipe symbol). The following code works for me:
```{r echo=FALSE}
library(knitr)
library(kableExtra)
options(knitr.kable.NA = '')
dt <- mtcars[1:5, 1:6]
kable(dt, "latex", booktabs=T) %>%
kable_styling(full_width = F, position = "left") %>%
row_spec(0, align = "c",bold=T ) %>%
column_spec(1, bold = T) %>%
kable_as_image()
```
Ref: See page 24 here:
https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf
Following this example, here's what I have
# iris
This section is about the iris dataset
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(iris[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(mtcars[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
But the output looks like this:
How do make the mtcars section appear below the iris section?
I guess kableExtra doesn't offer such functionality yet.
You can however resolve to html, and do the following with little effort:
---
title: "hi"
author: "me"
date: "March 23, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
```
```{r}
kable(head(iris[, 1:2])) %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# iris
This section is about the iris dataset.
This could be a whole paragraph.
<p style="clear: both"></p>
```{r}
kable(head(mtcars[, 1:2])) %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset
The relevant bit is <p style="clear: both"></p>
Put text after the table, and also add line breaks. Since the output is in html, it seems like it doesn't know how large the table is, and uses simple HTML tags to wrap around. One solution, wrap it in a table with 100% width:
<table style="width:100vw">
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(iris[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# iris
This section is about the iris dataset.
</table>
<table style="width:100vw">
```{r, echo=FALSE, message=FALSE, warning=FALSE}
kable(head(mtcars[, 1:2]), format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE, position = "float_right")
```
# mtcars
This section is about the mtcars dataset.
</table>
I'm trying to fit large table of frequencies into my slide. There are many values, and even the rare ones would be nice to show.
I played with different options but none gives me a satisfactory solution. Here is Rmd so far:
---
title: "Untitled"
author: "author"
date: "date"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
df <- as.data.frame(table(rownames((USArrests))))
```
## table 1
```{r t1, echo = TRUE}
table(rownames((USArrests)))
```
## table 2
```{r t2}
library(knitr)
library(kableExtra)
kable(df, "html") %>%
kable_styling(bootstrap_options = "striped", font_size = 10)
```
Table 1 doesn't fit:
Table 2 could be squeezed but with tiny font, and lots of wasted space on the sides.
I also looked into pander, xtable and stargazer but failed to find solution from them either.
Any other alternatives?
You could spread your table across multiple columns to fit the space. In my example below, I split the frame up into 3 pairs of columns with uneven length.
---
output: ioslides_presentation
---
```{r setup, include=FALSE}
library(dplyr)
library(magrittr)
library(knitr)
library(kableExtra)
```
## table 1
```{r, echo=TRUE, eval=FALSE}
USArrests %>% rownames %>% table
```
```{r, echo=FALSE}
df <- USArrests %>%
rownames %>%
table %>%
as_tibble
df %$%
tibble(
name1 = `.`[1:17],
n1 = n[1:17],
name2 = `.`[18:34],
n2 = n[18:34],
name3 = c(`.`[35:50], ""),
n3 = c(n[35:50], "")
) %>%
kable("html", align = c("l", "c"), col.names = rep(c("Name", "Frequency"), 3)) %>%
kable_styling(bootstrap_options = c("striped", "condensed"), font_size = 18)
```
N.B. I accept the transform step into multiple columns could have been done more elegantly and providing and more programmatic solution, however, I'll leave that to others to refine.