将文字说明放在kable附近

时间:2018-03-23 20:10:42

标签: r r-markdown knitr kable kableextra

关注this example,这里有我的

# 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")
```

但输出如下:

enter image description here

如何让 mtcars 部分出现在 iris 部分下方?

2 个答案:

答案 0 :(得分:2)

我猜kableExtra还没有提供此类功能。

然而,你可以解析为 html ,并且只需付出一点努力就可以完成以下任务:

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

相关位为<p style="clear: both"></p>

答案 1 :(得分:1)

在表格后放置文字,并添加换行符。由于输出是html,似乎它不知道表的大小,并使用简单的HTML标记来包装。一种解决方案,将其包装在100%宽度的表中:

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