【问题标题】:How to align table left in HTML output using R Markdown and Knitr如何使用 R Markdown 和 Knitr 在 HTML 输出中左对齐表格
【发布时间】:2020-05-20 18:37:00
【问题描述】:

我正在 RStudio 中尝试使用 Knitr/KableExtra,但无法让我的表格使用网络浏览器的全宽或控制屏幕上的表格对齐方式。

下面是代码示例,根据 kable_styling 文档,我尝试强制表格与屏幕左侧对齐,但在 html 输出中,表格始终居中。它看到左侧有一个我无法使用的不可见边距。当我有一个包含更多字段的表格时,就会出现问题......左侧的大边距仍然存在,迫使表格延伸到屏幕右侧并生成一个水平滚动条 - 非常烦人和丑陋。

有什么方法可以使用左边距或强制表格真正向左对齐?

这是一个问题的例子:

---
title: "Untitled"
author: "ME"
date: "2/4/2020"
output: html_document
---

```{r setup, include=FALSE}
library(kableExtra)

knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
x_html <- knitr::kable(head(mtcars), "html")
kable_styling(x_html, "striped", position = "left", font_size = 7)
```

【问题讨论】:

    标签: r knitr kable kableextra


    【解决方案1】:

    您需要调整默认的 CSS 主题。例如,让内容以 100% 的可用宽度显示:

    
    ```{css}
    .main-container {
        max-width: 100%;
    }
    ```
    
    

    还有其他解决方案,但这个可能是最简单的:

    
    ---
    title: "Untitled"
    author: "ME"
    date: "2/4/2020"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    library(kableExtra)
    
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{css}
    .main-container {
        max-width: 100%;
    }
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r cars}
    x_html <- knitr::kable(head(mtcars), "html")
    kable_styling(x_html, "striped", position = "left", font_size = 7)
    ```
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 2016-10-03
      • 2014-02-19
      • 1970-01-01
      • 2023-03-04
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多