xtable + knitr设置列宽html

Tyl*_*ker 5 html column-width xtable knitr

如何knitr使用xtable包设置代码块的(Rmd)输出中各列的列宽?

MWE

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

```{r, results="asis", echo=FALSE}
print(xtable(mtcars[1:2, 1:2]), type="html", include.rownames=FALSE)
```
Run Code Online (Sandbox Code Playgroud)

让我们说我想制作#1 - 2英寸宽的柱子和#2 - 3英寸宽的柱子.

我没有在xtable这里结婚,但不知道任何其他html表格包可以做到这一点.

Ric*_*ard 1

您可以更改 xtable 使用的 css 来格式化表格并更改列宽。但它不允许更改单个列。

请参阅http://nsaunders.wordpress.com/2012/08/27/custom-css-for-html- generated-using-rstudio/

下面是一个例子:

将样式表(此处名为 custom.css)添加到与 Markdown 文件相同的文件夹中。

table {
   max-width: 95%;
   border: 1px solid #ccc;
 }

th {
  background-color: #000000;
    color: #ffffff;
    width: 100px;
}

td {
  background-color: #dcdcdc;
  width: 100px;
}
Run Code Online (Sandbox Code Playgroud)

并设置使用此样式表的选项

```{r setup, include=FALSE}
library(xtable)
options(rstudio.markdownToHTML = 
       function(inputFile, outputFile) {      
       require(markdown)
       markdownToHTML(inputFile, outputFile, stylesheet='custom.css')   
      }
)

```

```{r, results="asis", echo=FALSE}
print(xtable(mtcars[1:2, 1:2]), type="html", include.rownames=FALSE)
```
Run Code Online (Sandbox Code Playgroud)

或许可以破解 print.xtable 函数以获得更大的灵活性。