Chapter 9 Tables

9.1 DataTable basic

  • We usually plot tidy data where the data frame is “long”. But for easy to read tables we will often want to reshape our long data to “wide”. In the Texas housing sales data, this means showing one column per month (rather than one row per month in tidy data).

  • To make the data wide we use tidyr::pivot_wider() then pipe that wide data into DT::datatable() to create an interactive table very easily.

9.2 DataTable (bells & whistles)

  • The DT DataTables reference describes all the DataTable elements you can change with clear examples and code. This resource is usually all you need to refer to.

  • Eventually you will have chosen lots of different DataTable options to apply. However, if your work includes many tables, this will lead to lots of repeated settings code. Reduce the amount of code by setting the default table options globally like this.

  • The default settings above will then be applied every time you use DT::datatable()

9.3 Colour DataTable with formattable

  • Colouring DataTable cells based on the value it contains is a common client request. Here is a code template you can re-use that is like Excel’s conditional formatting.

9.4 rpivottable basic

  • I love Excel pivot tables and charts for exploring data. rpivotTables are a similar exploratory tool in R. We simply put the dataframe into the function rpivotTable::rpviotTable() then play around with the html output below (as you would with an Excel pivot table).

9.5 rpivottable (bells & whistles)

  • Once we’ve decided how we want the rpivotTable to look manually we can hard code the layout to tell a particular story for our audience.