How to create a table in Rstudio presentation

Glen picture Glen · Mar 25, 2015 · Viewed 8k times · Source

I'm trying to create a table in an RStudio .Rpres file. Below is what I have at this point from online searching but the alignment is not correct. Is this the best method? Any suggestions on the alignment?

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

  : Demonstration of simple table syntax.

Result:

enter image description here

Answer

Rentrop picture Rentrop · Mar 25, 2015

You can use knitr::kable to print your data.frame

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

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

@alignments: I tried using align = c('l', 'r', 'c', 'r', 'l') as described in ?kable but it did not work. Maybe this is a bug.

Output of

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  |