library(dplyr)
data2 <- data.frame(uf = sample(state.abb[1:8], 100, replace = TRUE))
data2 %>% group_by(uf) %>%
summarise(n = n(), ) %>%
arrange(desc(n))| uf | n |
|---|---|
| AL | 17 |
| CT | 15 |
| AK | 13 |
| CA | 13 |
| CO | 12 |
| AZ | 11 |
| DE | 11 |
| AR | 8 |
Andrés Gutiérrez
November 19, 2017
I am writing a Rmarkdown document with plenty of tables, and I want them in a decent format, e.g. kable. However I don’t want to format them one by one. For example, I have created the following data frame in dplyr
| uf | n |
|---|---|
| AL | 17 |
| CT | 15 |
| AK | 13 |
| CA | 13 |
| CO | 12 |
| AZ | 11 |
| DE | 11 |
| AR | 8 |
One solution to the output format of this data frame would be to name it as an object in R, and then give it a format by using the kable function.
| uf | n |
|---|---|
| AL | 17 |
| CT | 15 |
| AK | 13 |
| CA | 13 |
| CO | 12 |
| AZ | 11 |
| DE | 11 |
| AR | 8 |
However, if your document has hundreds of these queries and you need a faster way to compile the document, while keeping the kable style automatically, avoiding giving a name to the data frame and even avoiding to call the kable function over that name, you can use the printr package. Just add the following piece of code inside a chunk at the beginning of your document and voilá.
Now, all of your data frames will have a decent style, and you do not need to worry about this issue. For example, I have knitted a presentation by using printrand the first code in this post, and this is the result: