繁体   English   中英

如何使用 R Markdown 和 Knitr 对齐 HTML output 中的左侧表格

[英]How to align table left in HTML output using R Markdown and Knitr

我正在 RStudio 中试验 Knitr/KableExtra,但无法让我的表格使用 web 浏览器的全宽或控制屏幕上的表格 alignment。

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

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

这是问题的示例:

例子

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

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

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

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

凯特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM