Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating table outputs for PDF using R-markdown [closed]

Tags:

r

r-markdown

How can I create a formatted table for a PDF report using the kable function in Rmarkdown? Here is a MWE:

library(knitr)

df <- data.frame(bucket = 1:11,
                 value = c(-0.8125594, -0.7590050, -0.7189301, -0.7188391, -0.5047816,
                           -0.3439579, -0.4376782, -0.1300217, 0.9145718, 2.1844290,
                           4.8374356))

print(kable(df))

Output:

| bucket|      value|
|------:|----------:|
|      1| -0.8125594|
|      2| -0.7590050|
|      3| -0.7189301|
|      4| -0.7188391|
|      5| -0.5047816|
|      6| -0.3439579|
|      7| -0.4376782|
|      8| -0.1300217|
|      9|  0.9145718|
|     10|  2.1844290|
|     11|  4.8374356|

I am trying to format it for a PDF with a good visual like the picture attached:

Output that I want to see in PDF

like image 637
Bustergun Avatar asked Feb 16 '26 22:02

Bustergun


1 Answers

There is no need to wrap the kable function with print. Here is a basic RMarkdown document which demonstrates it:

---
title: "MWE"
output: pdf_document
---

```{r, message = FALSE}
library(knitr)

df <- data.frame(bucket = 1:11,
                 value = c(-0.8125594, -0.7590050, -0.7189301, -0.7188391, -0.5047816,
                           -0.3439579, -0.4376782, -0.1300217, 0.9145718, 2.1844290,
                           4.8374356))

kable(df)
```

enter image description here

If you need to further customise the table, I would recommend checking out the kableExtra package: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

like image 104
Michael Harper Avatar answered Feb 18 '26 12:02

Michael Harper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!