The goal of this tutorial is to introduce you to kableExtra
, which you can use to build common complex tables and manipulate table styles. It imports the pipe %>%
symbol from magrittr
and verbalizes all the functions in order to permit you to add “layers” to the kable output. In combination with R Markdown, you can create a nice PDF document with your table inside.
PACKAGE INSTALLATION & DATA FRAME
The first thing you have to do is install and load the packages. Moreover, we need a data-set to work with. The data-set we chose in our case is “mtcats”, which was extracted from the 1974 Motor Trend US magazine. It is comprised of fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). We will extract some rows and columns out of it to work with:
install.packages("kableExtra")
# For dev version
# install.packages("devtools")
devtools::install_github("haozhu233/kableExtra")
install.packages("knitr")
install.packages("rmarkdown")
library(knitr)
library(kableExtra)
library(rmarkdown)
dt <- mtcars[1:5, 1:6]
You can use head(mtcars)
in order to see the variables of your data-set.
Create PDF document
To create a PDF document from R Markdown, you specify the pdf_document output format in the front-matter of your document:
---
title: "Kable"
author: Makis
date: January 31, 2018
output: pdf_document
---
Specify Format
When you are using kable()
, if you don’t specify the format, by default, it will generate a markdown table and let pandoc handle the conversion from markdown to HTML/PDF. Rmarkdown doesn’t support complex tables. For example, if you want to have a double-row header table, markdown just cannot provide you the functionality you need.
As a result, when you have such a need, you should define the format in kable()
as either “html” or “latex”. In this tutorial, I’ll still put format=”latex” in the function in
case users just want to quickly replicate the results.
options(knitr.table.format = "latex")
- Build a complete workflow in R for your data science problem
- Get in-depth on how to report your results in an interactive way
- And much more
Simple LaTeX
Our first table looks quite simple in appearance.
kable(dt)
LaTeX Table With Booktabs
In order to make your table look cleaner, we will create book tabs simply by calling book tabs = T directly in kable().
kable(dt, format = "latex", booktabs = T)
Hold Position
A table environment will automatically find the best place to put your table. However, in many cases, you do want your table to appear in a position you want it to be. In
this case, you can use this “hold_position” option here.
kable(dt, caption = "Demo table", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position"))
Scale down
When you have a wide table that will normally go out of the page and you want to scale down the table to fit the page, you can use the “scale_down” option here. Note that, if your table is too small, it will also scale up your table.
#wide
kable(cbind(dt, dt, dt), format = "latex", booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down"))
#small
kable(cbind(dt), format = "latex", booktabs = T) %>%
kable_styling(latex_options = c("striped", "scale_down"))
Repeat Header in Longtable
In kableExtra 0.3.0 or above, a new option, “repeat_header”, was introduced into kable_styling. It will
add header rows to longtables spanning multiple pages. For table captions on following pages, it will
append “continued” to the caption to differentiate.
If you want to completely replace the table caption instead of appending, you can specify it in the option:
repeat_header_method.
long_dt %
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("repeat_header"))
Full Width
If you have a small table and you want to spread it wide on the page, you can try the “full_width” option. Unlike scale_down, it won’t change your font size. You can use column_spec
together with “full_width” to achieve the best result.
kable(dt, format = "latex", booktabs = T) %>%
kable_styling(full_width = T)
Position
Table Position only matters when the table doesn’t have full_width
. You can choose to align the table to the center or left side of the page. The default value of the position is center.
Note that even though you can select to right align your table, your table will actually be centered.
kable(dt, format = "latex", booktabs = T) %>%
kable_styling(position = "center")
Font Size
If one of your tables is huge and you want to use a smaller font size for that specific table, you can use the “font_size” option.
kable(dt, format = "latex", booktabs = T) %>%
kable_styling(font_size = 7)
Now, let’s move on to the first set of real exercises on the ggvis package!
Thank you for creating this tutorial, am studying right now.
Hi I am trying to add three level of headings above the table in r markdown pdf.
Can you please suggest me the way to do so?