如何在Rstudio演示文稿中创建表

时间:2015-03-25 21:53:05

标签: r rstudio r-markdown rpres

我正在尝试在RStudio .Rpres文件中创建一个表。以下是我在网上搜索时的情况,但对齐方式不正确。这是最好的方法吗?有关对齐的任何建议吗?

Test
=========================================================
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

  : Demonstration of simple table syntax.

结果:

enter image description here

3 个答案:

答案 0 :(得分:3)

您可以使用knitr::kable打印data.frame

Test
========================================================

```{r, echo=FALSE}
my_df <- iris
knitr::kable(head(my_df))
```

@alignments: 我尝试使用align = c('l', 'r', 'c', 'r', 'l')中所述?kable 但它不起作用。也许这是一个错误。

的输出
knitr::kable(head(iris), align = c('l', 'r', 'c', 'r', 'l'))

|Sepal.Length | Sepal.Width| Petal.Length | Petal.Width|Species |
|:------------|-----------:|:------------:|-----------:|:-------|
|5.1          |         3.5|     1.4      |         0.2|setosa  |
|4.9          |         3.0|     1.4      |         0.2|setosa  |
|4.7          |         3.2|     1.3      |         0.2|setosa  |
|4.6          |         3.1|     1.5      |         0.2|setosa  |
|5.0          |         3.6|     1.4      |         0.2|setosa  |
|5.4          |         3.9|     1.7      |         0.4|setosa  |

答案 1 :(得分:2)

pander示例:

```{r}
df <- replicate(3, sample(letters, 3))
colnames(df) <- rep('foobar', 3)
pander::pander(df, justify = c('right', 'left', 'center'))
```

或者为所有列指定全局对齐(也可以是智能功能BTW):

```{r}
set.alignment('right')
pander::pander(df)
```

两者都会产生格式正确的降价表,可以在HTML中呈现。

答案 2 :(得分:1)

我设法通过在函数调用中包含align参数来使format = "html"工作,所以在FlooO上面讨论的示例中:

knitr::kable(head(iris), format = "html", align = c('l', 'r', 'c', 'r', 'l'))

给了我想要的结果