[英]How to align table left in HTML output using R Markdown and Knitr
I am experimenting with Knitr/KableExtra in RStudio but cannot make my tables use the full width of the web browser or control the table alignment on the screen.我正在 RStudio 中试验 Knitr/KableExtra,但无法让我的表格使用 web 浏览器的全宽或控制屏幕上的表格 alignment。
Below is an example of the code where as per the kable_styling documentation, I have tried force the table to align to the left of the screen but in the html output, the table is always centered.下面是代码示例,根据 kable_styling 文档,我尝试强制表格与屏幕左侧对齐,但在 html output 中,表格始终居中。 It sees that there is an invisible margin to the left that I can't use.
它看到左侧有一个我无法使用的无形边距。 The problem arises when I have a table with more fields....the large margin on the left remains, forcing the table to extend to the right of the screen and generate a horizontal scrollbar - very annoying and ugly.
当我有一个包含更多字段的表格时,问题就出现了......左边的大边距仍然存在,迫使表格延伸到屏幕右侧并生成水平滚动条 - 非常烦人和丑陋。
Is there some way I can use the space on the left margin or force the table to align truly to the left?有什么方法可以使用左边距的空间或强制表格真正向左对齐吗?
Here is an example of the issue:这是问题的示例:
---
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)
```
You need to tweak the default CSS theme.您需要调整默认的 CSS 主题。 For example, to make the content display on 100% of the available width:
例如,要使内容以 100% 的可用宽度显示:
```{css}
.main-container {
max-width: 100%;
}
```
There are other solutions but this one is probably the most easy:还有其他解决方案,但这个可能是最简单的:
---
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)
```
I think another way to fill in the left-hand side blank is to add Floating TOC option (right under html_document
), which will illustrate the table content of Rmarkdown (given that you used headers).我认为另一种填充左侧空白的方法是添加 Floating TOC 选项(在
html_document
下),这将说明 Rmarkdown 的表格内容(假设您使用了标题)。
output:
html_document:
toc: true
toc_float: true
---
Kate凯特
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.