DataTables is used ubiquitously to display tabular data interactively within webpages. DataTables allows entire tables or individual columns to be filtered. DataTables provides a number of extensions for adding additional features, including responsivenes
- [Instructor] Often, it's useful to provide interactive tables alongside charts, maps, or network visualizations so that viewers can get at the raw data underlying your analyses. The DT library is the single best htmlwidget for presenting your data in interactive tables. Let's look at what it can do. I'm going to open up in our exercise files example-tables.R, I'll minimize our environment. At the top of the script file, we load the tidyverse and the DT library. We import a CSV file with read_csv, and then on line seven, we view our table using the data table function.
And we can see, I had my columns here. I can horizontally scroll through my data, and I can vertically scroll through my data. I get automatic pagination, telling me I'm currently looking at observations one through 10 of 399 entries, and I can also filter this data globally, so I can search within this data set for TX i.e. for observations from Texas. Now, you might want to provide filters per column, and this is very easy to do in the DT library. Lines 10 through 12 enable filters at the top of each of the columns, so now I can filter the table by a single column, so I can say I only want observations where the sender country is DEU, and then any numeric columns provide a range slider to me.
So, I could say I'm only interested in observations between these latitudes here. There's also a date range slider provided for any columns which contain date data. There are a number of spreadsheet-like operations that data table supports as well. For instance, freezing columns or rows so as you scroll through the table, you keep the information about which record you're looking for. This is easily achieved on lines 16 through 25. We can see that the first two columns now are fixed, and as I horizontally scroll through the data, these two columns stay where they are.
Sometimes your data tables contain a huge number of columns, and you don't want users to see all of those columns, or you want them to be able to select which columns they see at any particular time. This is also possible in the DT library. If I run the code on lines 28 through 33, we can see I've enabled a button called Column visibility. If I click on this, this allows me to deselect columns from display. So now we can see the only columns I have on display are sender.country, receiver.latitude, longitude, date, and so on.
How about if you have a data table that you want to display on both desktop and mobile devices? Well, datatable has an extension called Responsive which allows you to easily design tables that work across devices. Let's see how such a table looks, and we can see the only columns which are displayed for me are those which fit within the width of my viewer, so sender.country, city, state, and sender.latitude. If I click on this little green plus here, then it shows me the columns which have been truncated.
I recommend that you play about with the maximum width the data table's allowed to be to ensure the best looking tables across the most number of devices. Now, Shiny is a framework for building interactive web applications using the R language, and the DT library provides good support for Shiny apps. You can easily extract the following information from a table. The selected row, column, or cell. You also have access to the current filter states and the page that you're currently on inside of a data table's pagination.
It's also possible to update datatables via proxy interface, meaning tables can be modified without having to rerender the entire table, which is expensive and often doesn't look good to the viewer. The following things can be achieved via the proxy interface. The selection of rows, the moving to a specific page, and the adding and removing of rows. Let's look at a quick Shiny app using the DT library in our exercise files. If I go to our file explorer and open up server.R and hit Run App, this will load our Shiny app.
We can see our Shiny app contains a data table here, and when I select a row, it tells me which row I've selected. Using Command + Click, I can select multiple rows, and I'm told exactly which rows I have selected here. I also have a text input here, which allows me to type a country. I'll type UK, and I'll type Oxford as my sender city, and if I click Add new dummy row, we can see that I've used the data table proxy method to add a row to my table.
I thoroughly recommend looking through the online documentation to learn more.
Start by learning to manage packages and structure data for visualizations with the tidyverse and the pipe operator. Then there is an important question: Which library should you choose? The course introduces five popular options: Leaflet, Plotly, Highcharter, visNetwork, and DataTables (DT). Instructor Martin Hadley shows how to use these libraries to create scattergeo, choropleth, and geolines maps; stacked bar charts, scatter charts, bubble charts, and heat maps; treemaps and time series charts; interactive networks and graphs; and responsive, interactive data tables. Plus, learn how to customize your visualizations with legends and tooltips, and extract click information for Shiny apps.
- Installing htmlwidgets
- Selecting the best htmlwidget
- Creating interactive maps with Leaflet
- Creating interactive charts with Plotly and Highcharter
- Creating interactive networks with visNetwork
- Creating interactive tables with DT