Table with Kable and Formattable

Is there a way to format tables using either Kable or Formattable to create table like this below.

Fmt

Does the output of this code satisfy you? It doesn't have the bold lines, and the surrounding box.

library(kableExtra)

dt <- data.frame(stringsAsFactors = FALSE,
           c("A", "A", "B", "B", "C", "C"),
           c(74, 52, 49, 68, 97, 23),
           c(36, 60, 24, 36, 75, 29),
           c(20, 16, 39, 98, 6, 64),
           c(43, 84, 3, 57, 35, 9),
           c(75, 36, 20, 70, 60, 37),
           c(31, 7, 40, 81, 65, 74))

names(x = dt) <- c("ProductID", "M", "F", "M", "F", "M", "F")

dt %>%
  kable(format = "latex") %>%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %>%
  add_header_above(c("", "Apple" = 2, "Orange" = 2, "Grapes" = 2)) %>%
  collapse_rows(columns = 1,
                valign = "middle")

WOW looks amazing !

Only thing is, I don't know if we can do this.

  • Front and the back borders are missing
  • Some borders thicker than the others
  • Header bold.

Other than that it looks FABULOUS.

I missed this one. The following should work:

library(kableExtra)

dt <- data.frame(stringsAsFactors = FALSE,
           c("A", "A", "B", "B", "C", "C"),
           c(74, 52, 49, 68, 97, 23),
           c(36, 60, 24, 36, 75, 29),
           c(20, 16, 39, 98, 6, 64),
           c(43, 84, 3, 57, 35, 9),
           c(75, 36, 20, 70, 60, 37),
           c(31, 7, 40, 81, 65, 74))

names(x = dt) <- c("ProductID", "M", "F", "M", "F", "M", "F")

dt %>%
  kable() %>%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %>%
  add_header_above(header = c("", "Apple" = 2, "Orange" = 2, "Grapes" = 2),
                   bold = TRUE) %>%
  collapse_rows(columns = 1,
                valign = "middle") %>%
  row_spec(row = 0,
           bold = TRUE)

Yes, I know and specified in my previous post. I don't know how to do it.

I don't follow this one.

I guess you wanted these as bullets. You need to add a space after *. I edited your post, and I hope you won't mind.

I'll suggest you to avoid name mentioning as far as possible. Please take a look here:

Alright, will not use @ and mention name.

I guess you wanted these as bullets. You need to add a space after *. I edited your post, and I hope you won't mind.

Don't mind !

I don't follow this one.
bd222222

Yes, I know and specified in my previous post. I don't know how to do it.

Alright I see now. sorry I didn't see that before, it still looks very nice.
Thank you !

You can do pretty amazing table styling using flextable if you are interested in other table :package:
https://davidgohel.github.io/flextable/articles/overview.html

Hi John!

I'm able to get the borders, as you may find below in the code. However, I couldn't get the thicker lines. From this post, I guess it may be done using CSS, which I don't know at all. You may be able to figure it out, and if you do, can I request you to share the solution?

This is my Rmd code:

---
title: "Untitled"
output: pdf_document
---

some text

```{r}
library(kableExtra)

dt <- data.frame(stringsAsFactors = FALSE,
                 c("A", "A", "B", "B", "C", "C"),
                 c(74, 52, 49, 68, 97, 23),
                 c(36, 60, 24, 36, 75, 29),
                 c(20, 16, 39, 98, 6, 64),
                 c(43, 84, 3, 57, 35, 9),
                 c(75, 36, 20, 70, 60, 37),
                 c(31, 7, 40, 81, 65, 74))

names(x = dt) <- c("ProductID", "M", "F", "M", "F", "M", "F")

dt %>%
  kable() %>%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %>%
  add_header_above(header = c("", "Apple" = 2, "Orange" = 2, "Grapes" = 2),
                   bold = TRUE,
                   border_left = TRUE,
                   border_right = TRUE) %>%
  row_spec(row = 0,
           bold = TRUE) %>%
  column_spec(column = 1,
              border_left = TRUE) %>%
  column_spec(column = 7,
              border_right = TRUE) %>%
  collapse_rows(columns = 1,
                valign = "middle")
```

some text

And, here's the output: try.pdf (127.5 KB).

Thank you, will look into Flextable.

Yarnabrina,

Your last code has an error.
Error in add_header_above. unused arguments border_left and border_right.

I understand what you mean, but I do not face this problem in my laptop. I tried in a fresh RStudio Cloud project, and I received the same error as you. But after I tried to install the development version using devtools::install_github("haozhu233/kableExtra"), the error vanished.

Right now, I can't recall whether I installed dev version in my laptop or not. I have version 1.1.0.9000, where CRAN lists as 1.1.0. Can you please try to install the dev version and see whether you get the error or not?

In my laptop, the following code creates two tables, one with border_left and border_right, and one without. Please try to run it after installing the dev version and you'll notice the change.

My Rmd Code
---
output: pdf_document
---

bla bla bla

```{r}
library(kableExtra)
dt <- data.frame(stringsAsFactors = FALSE,
                 c("A", "A", "B", "B", "C", "C"),
                 c(74, 52, 49, 68, 97, 23),
                 c(36, 60, 24, 36, 75, 29),
                 c(20, 16, 39, 98, 6, 64),
                 c(43, 84, 3, 57, 35, 9),
                 c(75, 36, 20, 70, 60, 37),
                 c(31, 7, 40, 81, 65, 74))
names(x = dt) <- c("ProductID", "M", "F", "M", "F", "M", "F")
```

loading `kableExtra` and preparing data

```{r}
dt %>%
  kable() %>%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %>%
  add_header_above(header = c(" " = 1, "Apple" = 2, "Orange" = 2, "Grapes" = 2),
                   bold = TRUE,
                   border_left = TRUE,
                   border_right = TRUE) %>%
  row_spec(row = 0,
           bold = TRUE) %>%
  column_spec(column = 1,
              border_left = TRUE) %>%
  column_spec(column = 7,
              border_right = TRUE) %>%
  collapse_rows(columns = 1,
                valign = "middle")
```

above table is created with border_left and border_right as TRUE

```{r}
dt %>%
  kable() %>%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %>%
  add_header_above(header = c(" " = 1, "Apple" = 2, "Orange" = 2, "Grapes" = 2),
                   bold = TRUE) %>%
  row_spec(row = 0,
           bold = TRUE) %>%
  column_spec(column = 1,
              border_left = TRUE) %>%
  column_spec(column = 7,
              border_right = TRUE) %>%
  collapse_rows(columns = 1,
                valign = "middle")
```

above table is created without border_left and border_right

Choice of package may depend on output. I've found Huxtable is good package for fairly consistent output to pdf and html.

1 Like

I put the code devtools::install_github("haozhu233/kableExtra") after library(kableExtra) and I got an error message, there is no package called 'devtools'

Do you know how we can format kableExtra table or move it to anywhere in the PDF we want, such as the left, the top, the right or the bottom. I have a big table and it looks like it's crooked, more to the right than the left. kableExtra seems very limited it terms of what it can do. Pure Latex have more option in terms of formatting but I haven't seen it work with data frame.

Have you installed devtools earlier? Run this code, and hopefully it'll solve this problem:

install.packages("devtools")
devtools::install_github("haozhu233/kableExtra")
library(kableExtra)

Please go through the documentation and the vignettes. There is an argument position in kable_styling:

position A character string determining how to position the table on a page. Possible values include left , center , right , float_left and float_right . Please see the package doc site for demonstrations. For a LaTeX table, if float_* is selected, LaTeX package wrapfig will be imported.

Undoubtedly, you'll get more control if you use LaTeX. When you use kableExtra, it'll generate a LaTeX code, which will then be processed. So, if you're comfortable with LaTeX (I'm not), why don't you use LaTeX directly? Or, you can use kableExtra to generate the primary LaTeX code, and then store the tex file. Then modify suitably in that file using pure LaTeX and render that finally.

Thanks for the update. The code still gives error messages after installing what you said.

why don't you use LaTeX directly?

I don't know how to work with it on data frame or variables, unless the numbers are static in which case my numbers aren't.

I’m still trying to sample the different concepts. Thanks for sharing. How can I generate xtable or KableExtra latex codes and store it as tex file or is it textfile, then read it in ?

In the Rmd file, change the YAML section to keep the tex file like this:

output: 
  pdf_document: 
    keep_tex: yes

You can also do the same from the settings (Gear -> Output Options -> Advanced).

Then knit the file and it'll create the tex file in the directory of the Rmd file. Open that in RStudio, make necessary modifications, and use Compile PDF button.

It’s good to know but I think is limited since it’ll create the Latex with the kable data frame data and the formatting will be based on your formatting with kable which is limited and if you want change you still would have to make changes to the codes it produced. However is a good way to learn latex code.

I thought of another concept, create text file of data frame then format the latex code with the file it reads in.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.