Make nice tables

Try to make the table that is already included in your document more beautiful.

My tip is to first save the table in a variable. Then you can pass this variable into the table functions:

penguins_sum <- penguins %>%
  filter(!(is.na(sex))) %>%
  group_by(species, sex) %>%
  summarize(
    bill_length = mean(bill_length_mm, na.rm = TRUE),
    bill_depth = mean(bill_depth_mm, na.rm = TRUE),
    flipper_length = mean(flipper_length_mm, na.rm = TRUE),
    body_mass = mean(body_mass_g, na.rm = TRUE)
  )
penguins_sum

2.1 Basic styling with kable

Start with the kable function. Before you use it, load the knitr package in the setup chunk.

You can for example:

Knit the document to different target output formats and see how the table looks like.

2.2 Advanced styling with kableExtra

The following now only works for PDF and HTML output. If you want to keep knitting to word, you later have to switch back to only using kable.

Don’t forget to load the kableExtra package in the setup chunk.

Start adding extra styling to the table. For example, you can

Have a look at ?kable_styling for more options.

Extras

If you still have time, have a look at the package documentation of kableExtra for PDF tables or HTML tables to find many more options and examples.