5

パッケージknitrを使用してコード チャンクの (Rmd) 出力で個々の列の列幅を設定するにはどうすればよいですか?xtable

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 テーブル出力パッケージを知りません。

4

1 に答える 1

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 関数をハックして柔軟性を高めることができるかもしれません。

于 2014-01-06T22:44:09.080 に答える