In the previous post, I managed to create a table that was not just a simple Markdown table but one that was more dynamic or interactive. How I went about it, I will try to explain here. After a bit of googling, I realized that what I was asking for was DataTables. But since I am curious about R and RStudio, I wanted to try to create a corresponding table in this way and not by using JavaScript or Custom Style Sheets (CSS) as the page indicates. However, I am a beginner when it comes to both R and RStudio, and especially when it comes to how to import data sets to work from, I do not understand much. Therefore, I thought it would be easiest to start the work of producing an interactive table by importing the existing table in Excel format. (The current table is available by clicking “Översiktstabell och lista på indelning 2017 (Excel, nytt fönster)” on this page.)

Here it is described how Excel tables are imported into RStudio. This is done by selecting Import Dataset in the File menu, and then scrolling to the Excel sheet you want to import. When this is done, the code in RStudio looks like this:

1
2
3
4
> library(readxl)
> O_versiktstabell_och_lista_Kommungruppsindelning_2017_reviderad <- read_excel("~/Documents/phd/thesis/artikel2/Översiktstabell och lista_Kommungruppsindelning 2017 reviderad.xlsx", 
+     sheet = "Lista Kommungruppsind 2017")
> View(O_versiktstabell_och_lista_Kommungruppsindelning_2017_reviderad)

To create what looks like DataTables, an R package called DT is needed. To achieve the table we are looking for, write the following code in RStudio in direct connection to the previous code:

1
2
> library(DT)
> datatable(O_versiktstabell_och_lista_Kommungruppsindelning_2017_reviderad)

To then export this table, select Export followed by Save as Web Page in the menu just above the window where the table is visible.

Done.

P.S. There are also ways of making these tables downloadable. However, I don’t see a need for that at the moment.