Fancifying Excel tables in R Markdown with kable_extra

I’m a big fan of automating all the things. Especially when it comes to teaching materials. I build my own course websites for each class I teach, where I post lecture materials, due dates and links to external resources. Posting it online instead of my campus’s LMS makes the materials accessible to not only my current students, but past and future students as well.

I need a schedule, so do my students. I’m not good at winging things. I want to know what’s coming up this week, and next so I can plan my time accordingly. Again, so do my students. The easiest way for me to manage a schedule of topics, assignments, exams, projects for an entire 16 week class (or more) is in Excel, but that’s far from web-ready.

This is a record of how I turned an Excel sheet into a nicely formatted web-ready schedule using RMarkdown.

Here is the structure of my excel file. Week number, date, day of the week, the topics we’re covering, what the students can do to prep for class, what i’m going to evaluate them on (HW, exams), and notes about the assignment. Things in green are for my eyes only. Note the asterisk between items in the cells. The line breaks are for easy visual separation of items in a list, the asterisk will be used in code to create a formal html list.

So first let’s load the necessary libraries and set options for kable

library(dplyr);library(readxl); library(kableExtra); library(stringr)
options(knitr.table.format = "html", knitr.kable.NA = '') 

Read the data into rawdat, reformat the date, make copies of the columns that need list processing: topic, prep, eval. The filter is leftover from the Spring semester, after each exam I hide the weeks prior to that exam so that the most current and upcoming weeks are presented at the top of the table.

rawdat <- read_excel("schedule_615.xlsx")
cdat   <- rawdat %>% mutate(Date = format(date, "%m/%d"), 
                            Topics = topic, Preparation = prep, Evaluation = eval) %>% 
              filter(as.Date(date) > "2017-04-01") %>%
              select(Wk, Date, Topics, Preparation, Evaluation, topic, prep, eval)
  • First I find the rows that have some content in them, store this as topiclist. This ensures that I don’t get a list of *NA’s.
  • Then I turn those three columns into formal html lists. I paste0 the content of topic between starting <ul><li> and ending </li></ul> tags.
  • Lastly I use str_replace from the stringr package to replace the asterisk between list items with `
  • to end the first list item and start a second. Notice that there is no asterisk in the excel file in front of the first list item.
# parse topics - same code for all three vars. Yes I could have applied a function. 
topiclist <- which(!is.na(cdat$topic))
cdat$Topics[topiclist] <- paste0("<ul><li>", cdat$topic[topiclist], "</li></ul>")
cdat$Topics[topiclist] <- str_replace(cdat$Topics[topiclist], "\\*", '</li><li>')

Now I clean up the data frame using select from dplyr to only the variables I want to show, in the order I want to show. Then wrap kable on the entire table. * escape=FALSE is needed to process the html tags * kable_styling adds some very nice customizations to the table, including a non-custom css method to adjust column spacing and row shading behavior using hover, striped and responsive so that the table maintains it’s width on a cell phone, but lets you scroll to the side to see the whole table.

out <- select(cdat, Wk, Date, Topics, Preparation, Evaluation)

knitr::kable(out, align="l", escape=FALSE) %>% 
  kable_styling(bootstrap_options = c("hover", "striped", "responsive")) %>% 
  column_spec(1, width="1%") %>% column_spec(2, width="1%") %>%
  column_spec(3, width="20%") %>% column_spec(4, width="30%") %>%
  column_spec(5, width="30%")

The results can be seen on my MATH 615 course website

kable_extra can apply a lot of neat table customization features that make creating professional tables in R markdown an actual possibility (without switching to \(\LaTeX\)\Sweave) I highly recommend you checking it out http://haozhu233.github.io/kableExtra/

Having a nice automated framework like this makes my day-to-day life during the semester much easier. I just copy the schedule.Rmd file into another class’s repo and just change the excel file that is read in. Make adjustments to the excel file as needed, rebuild and git push. (Typically 5 min before class to fix a typo)

Session Info

Note that since i’m using blogdown to write this post, many more packages are loaded than what is necessary for the topic of this post.

sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 15063)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] methods   stats     graphics  grDevices utils     datasets  base     
## 
## other attached packages:
## [1] stringr_1.2.0         kableExtra_0.3.0.9000 readxl_0.1.1         
## [4] dplyr_0.5.0          
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.8     bookdown_0.4.1  digest_0.6.12   rprojroot_1.2  
##  [5] assertthat_0.1  R6_2.1.2        DBI_0.5-1       backports_1.0.5
##  [9] magrittr_1.5    evaluate_0.10   httr_1.2.1      blogdown_0.0.50
## [13] stringi_1.1.1   xml2_1.1.1      rmarkdown_1.6   tools_3.3.1    
## [17] readr_1.1.1     hms_0.2         yaml_2.1.14     rvest_0.3.2    
## [21] htmltools_0.3.6 knitr_1.16      tibble_1.3.0