如何在R Markdown和Knitr中使用HTML输出左对齐表格

3
我正在尝试在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)
```

2个回答

4

您需要调整默认的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)
```


0

我认为填写左侧空白的另一种方法是添加浮动目录选项(就在html_document下面),这将说明Rmarkdown的表格内容(假设您使用了标题)。

output:
  html_document:
     toc: true
     toc_float: true
---

Kate


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接