3

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

Glen
  • 1,722
  • 3
  • 18
  • 25

3 Answers3

4

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  |
Rentrop
  • 20,979
  • 10
  • 72
  • 100
2

A pander example:

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

Or specifying a global alignment for all columns (which can be a smart function as well BTW):

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

Both results in a correctly formatted markdown table that renders fine in HTML.

daroczig
  • 28,004
  • 7
  • 90
  • 124
1

I managed to get align to work by including the format = "html" parameter in the function call, so in the example discussed above by FlooO:

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

gave me the desired result

Valeri Voev
  • 1,982
  • 9
  • 25