【问题标题】:xtable + knitr set column width htmlxtable + knitr 设置列宽html
【发布时间】:2013-12-04 19:23:36
【问题描述】:

如何使用xtable 包在代码块的knitr (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)
```

假设我想让 column_#1 - 2 英寸宽和 column_#2 - 3 英寸宽。

我没有与xtable 结婚,但不知道有任何其他 html table out 包可以做到这一点。

【问题讨论】:

    标签: html knitr xtable column-width


    【解决方案1】:

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

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

    以下示例:

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

    table {
       max-width: 95%;
       border: 1px solid #ccc;
     }
    
    th {
      background-color: #000000;
        color: #ffffff;
        width: 100px;
    }
    
    td {
      background-color: #dcdcdc;
      width: 100px;
    }
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 2018-06-29
      • 2013-05-10
      • 1970-01-01
      相关资源
      最近更新 更多