Put text description adjacent to kable - r

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>

Related

add "continued" to long table caption using flextable in docx r

I knit code below. and this is the output. long in kbl(0 for html, I want to have the term "continued" in flextable for docx too. what is the trick?
---
title: ''
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(flextable)
library(dplyr)
library(ggplot2)
mpg[, 1:4] %>%
flextable() %>%
set_caption(caption = "my caption") %>%
set_table_properties(width = 1, layout = "autofit")
```

knitr::kable(x, col.names=NULL) doesn't works when the output is a pdf

I need to remove the name of variables in a table produced with kable and that I will export to pdf
kable(x, col.names = NULL) it works when the output is in html format, but when the output is pdf it does not compile and throws an error: "add_intent_latex..."
Try this solution, I prepared a template for you:
---
title: "Hello World"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include = FALSE}
library(kableExtra)
my_data <- mtcars[1:5, 1:6]
my_table <- my_data %>%
kbl(caption = "without col.names", col.names = NULL) %>%
kable_styling(bootstrap_options = c("striped", "hover"), latex_options = "hold_position")
my_table1 <- my_data %>%
kbl(caption = "with col.names") %>%
kable_styling(bootstrap_options = c("striped", "hover"), latex_options = "hold_position")
```
```{r}
my_table
```
```{r}
my_table1
```

Kable caption in rmarkdown file in HTML in bold

I want to make my table caption in bold but can't seem to find the option for it.
My code is (in a rmarkdown document):
kable(head(iris), caption = 'I want this in Bold') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
The output is:
Does this markdown-oriented solution work for you?
```{r, results='asis'}
kable(head(iris), caption = '**I want this in Bold**') %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed","responsive"))
```
for html-output this should work:
```{r, results='asis'}
kable(head(iris), caption = '<b>I want this in Bold</b>', format = 'html') %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed","responsive"))
```
for pdf-output this should work:
```{r, results='asis'}
kable(head(iris), caption = '\\textbf{I want this in Bold}', format = 'latex') %>%
kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed","responsive"))
```

can't align rows with kableExtra `cell_spec()`

---
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>

how to automatically rename columns with math mode?

Consider this simple example
---
title: "Untitled"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
```
## Slide with R Output
```{r , echo = FALSE ,warning = FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
cars %>%
filter(dist < 5) %>%
kable('latex', booktabs = T, escape = F, col.names = c( "$\\alpha$" , "$\\beta$" ) ) %>%
kable_styling(latex_options = c("striped", "hold_position"),
full_width = T)
```
Now this correcly generates the following output
The issue is that the renaming in col.names is manual and very tedious when my dataframe has many columns.
I would like to be able to able to say " if you see this variable dist in the dataframe, then map it to $\alpha$. Otherwise leave as is.
How can this be done?
Note, I am rendering the file using rmarkdown::render()
Thanks!!

Resources