【问题标题】:How to create a table in Rstudio presentation如何在 Rstudio 演示文稿中创建表格
【发布时间】:2015-05-29 19:23:44
【问题描述】:
我正在尝试在 RStudio .Rpres 文件中创建一个表。以下是我在网上搜索的内容,但对齐不正确。这是最好的方法吗?对对齐有什么建议吗?
Test
=========================================================
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
: Demonstration of simple table syntax.
结果:
【问题讨论】:
标签:
r
rstudio
r-markdown
rpres
【解决方案1】:
我设法通过在函数调用中包含format = "html" 参数来让align 工作,所以在上面FlooO 讨论的示例中:
knitr::kable(head(iris), format = "html", align = c('l', 'r', 'c', 'r', 'l'))
给了我想要的结果
【解决方案2】:
pander 示例:
```{r}
df <- replicate(3, sample(letters, 3))
colnames(df) <- rep('foobar', 3)
pander::pander(df, justify = c('right', 'left', 'center'))
```
或为所有列指定全局对齐方式(顺便说一句,这也可以是一个智能函数):
```{r}
set.alignment('right')
pander::pander(df)
```
两者都会生成格式正确的降价表,在 HTML 中呈现良好。
【解决方案3】:
您可以使用knitr::kable 打印您的data.frame
Test
========================================================
```{r, echo=FALSE}
my_df <- iris
knitr::kable(head(my_df))
```
@对齐:
我尝试使用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 |