简体   繁体   中英

How to create a table in Rstudio presentation

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:

在此输入图像描述

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  |

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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