繁体   English   中英

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

[英]How to create a table in Rstudio presentation

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

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

  : Demonstration of simple table syntax.

结果:

在此输入图像描述

您可以使用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  |

一个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中呈现。

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

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

给了我想要的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM