1. Beautiful tables
There's one last thing you are going to explore in this chapter: beautifying table output.
2. <<<New Slide>>>
Let's have a look at how data frames or tables are usually rendered in RMarkdown documents. The answer is simple: Just as they are rendered in the console. And that's far from optimal.
3. <<<New Slide>>>
For example, when you display that table on a mobile screen, rows will be split between multiple lines, and generally, it's hard to grasp the structure of the table without proper formatting.
4. YAML header to the rescue!
But do not despair! You can actually specify in the YAML header how data frame output should be rendered, using the df_print key. This key accepts multiple values, but the most sensible is either kable, which will use the kable function from the knitr package to beautify tables. The other one is paged, where the resulting data frame is paginated, so readers can switch between pages of the table. This of course only makes sense if the table is really large.
5. The second option
There's also a second option for beautifying tables with the kable function. The df_print key in the YAML header changes all tables in your document. If you want to beautify only one table, it is best to call the kable function directly on the data frame. Just pipe the current data frame directly into a kable function call. Notice that you need to specify the knitr package namespace before the function call for some reason.
6. Styling tables
Tables can be styled with CSS selectors too, of course. But in order to do that, you need to know a bit about the structure of HTML tables.
7. Styling tables
Roughly, tables can be separated into a table header section, wrapped in the thead tag, and a table body section, wrapped in the tbody tag. These two sections each contain a number of rows, denoted with the tr tag, standing for table row.
8. Styling tables
The table header section usually contains only one table row, while the tbody usually contains more than one row.
The table rows themselves can contain an arbitrary number of cells, denoted with th for table header cells and td for normal cells. Usually the amount of cells per row is consistent across the table.
With this knowledge, you should be able to customize your beautified table even more!
9. Let's practice!
Time to put this into practice and conclude this course with a few exercises on tables.