Using R Markdown to create PDF files for sharing, you can chose several different options for printing tables of data: default, kable and tibble. If you are using HTML, you have another option called paged, which allows you to specify the number of rows and columns to display, and the viewer can page between pages without having to scroll up and down a large table. I’ll cover that option in a future post.
To use the default style for PDF output, don’t include a df_print option in your R-Markdown YAML header section. To use kable or tibble add the df_print option like below and follow it with the option you want to use. Be sure to watch the indentation of the df_print line.
--- title: "Table example" output: pdf_document: df_print: kable/tibble ---
Here are examples of the three outputs:
Fig 1 . Traditional table output
Fig 2. Kable table output
If you do use kable, be aware that on wider tables the results may be cut off due to page sizes. To fix that, you will need to include the kableExtra and magrittr libraries. Then wrap the table function like this:
kable(head(bike)) %>% kable_styling(latex_options = c("striped","scale_down"))
Finally, although the latex_option is called “scale_down”, what it does is make your table fit within the margins of your document. On tables that already fit comfortably, using this function will enlarge them to fit from left to right margin. Below is what the output looks like using kable for a wide data set, first using the default kable settings, and then using the kable_styling:
Fig 4 . Table without kable_styling and with kable_styling
I included the option striped as well, which subtly highlights every other line for easier viewing.
The R logo is © 2016 The R Foundation. Used under terms of the CC-BY-SA 4.0 license.