【问题标题】:Put text description adjacent to kable将文字描述放在 kable 旁边
【发布时间】:2018-03-23 20:10:42
【问题描述】:

关注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")
```

但输出看起来像这样:

如何使 mtcars 部分显示在 iris 部分下方?

【问题讨论】:

    标签: r r-markdown knitr kable kableextra


    【解决方案1】:

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

    相关位是&lt;p style="clear: both"&gt;&lt;/p&gt;

    【讨论】:

    • +1 但我必须取消选择这个作为我选择的答案。如果表格很大,则此方法将表格置于标题和文本上方,这是不可取的。这是一个快速而肮脏的解决方案的好技巧,但我怀疑使用一些我不知道的 CSS 技巧有更好的解决方案。谢谢
    • @Ben 我不完全确定 - 您是说您希望根据大小将文本放在上方或并排放置?那可能是另一个问题。无论如何,这里有一个指向 css 教程 w3schools.com/css/css_float.asp 的链接,基本上讨论的是 floatclear
    • cbind(iris, iris) 代替iris[, 1:2] 试试你的解决方案,你就会明白我在说什么。当表格位于该部分的标题上方时,看起来很奇怪。 (感谢您对此的帮助)。我大概可以从这里弄清楚。
    【解决方案2】:

    将文本放在表格之后,并添加换行符。由于输出是 html,它似乎不知道 table 有多大,并使用简单的 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>
    

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2014-09-02
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多