3

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

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Bustergun
  • 977
  • 3
  • 11
  • 17
  • Try [`kable`](http://127.0.0.1:14557/library/knitr/html/kable.html) from `knitr` R package. – patL Feb 23 '18 at 07:52
  • 1
    Hi @Bustergun, I noticed you recently joined Stackoverflow. I would recommend checking out the guides on how to ask good questions: https://stackoverflow.com/help/how-to-ask . Also check this guide out on how to make a Minimal Working Example (MWE) https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Michael Harper Feb 23 '18 at 10:00
  • 1
    To help you see what I mean by a minimal working example, I have edited the question to remove all the code which wasn't required. Hope it helps in your understanding :) – Michael Harper Feb 23 '18 at 10:01

2 Answers2

8

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

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
1

You can use tableHTML to create flexible HTML tables that can be styled using CSS.

library(tableHTML)

First, create a tableHTML with specific column-widths, the caption, and 'scientific' theme (The default text align for that theme is 'center', this is why it needs to be changed to 'right' using add_css_conditional_column() and add_css_header()). The last thing is to left align the caption and add a margin:

df %>% 
  tableHTML(rownames = FALSE,
            widths = c(50, 100),
            theme = 'scientific',
            caption = 'Table 4: Bucket Table') %>% 
  add_css_conditional_column(columns = 1:2,
                             conditional = "between",
                            between = c(-Inf, Inf),
                            css = list(c("text-align"),
                                       c("right !important"))) %>% 
  add_css_header(css = list(c("text-align"),
                           c("right !important")),
                 headers = 1:2) %>% 
  add_css_caption(css = list(c("text-align", "margin-bottom"),
                             c("left", "10px")))

This is what the result looks like:

tableHTML_output

clemens
  • 6,653
  • 2
  • 19
  • 31