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
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
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
- Then I turn those three columns into formal html lists. I
paste0the content of
- Lastly I use
stringrpackage 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
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)
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.
## R version 3.4.3 (2017-11-30) ## Platform: x86_64-w64-mingw32/x64 (64-bit) ## Running under: Windows 10 x64 (build 16299) ## ## Matrix products: default ## ## locale: ##  LC_COLLATE=English_United States.1252 ##  LC_CTYPE=English_United States.1252 ##  LC_MONETARY=English_United States.1252 ##  LC_NUMERIC=C ##  LC_TIME=English_United States.1252 ## ## attached base packages: ##  methods stats graphics grDevices utils datasets base ## ## other attached packages: ##  stringr_1.2.0 kableExtra_0.7.0.9000 readxl_1.0.0 ##  dplyr_0.7.4 ## ## loaded via a namespace (and not attached): ##  Rcpp_0.12.15 xml2_1.1.1 knitr_1.18 ##  bindr_0.1 magrittr_1.5 hms_0.4.0 ##  munsell_0.4.3 rvest_0.3.2 viridisLite_0.3.0 ##  colorspace_1.3-2 R6_2.2.2 rlang_0.1.6 ##  plyr_1.8.4 httr_1.3.1 tools_3.4.3 ##  xfun_0.1 htmltools_0.3.6 yaml_2.1.16 ##  rprojroot_1.3-2 digest_0.6.14 assertthat_0.2.0 ##  tibble_1.4.2 bookdown_0.7 bindrcpp_0.2 ##  readr_1.1.1 glue_1.2.0 evaluate_0.10.1 ##  rmarkdown_1.8 blogdown_0.5 stringi_1.1.6 ##  compiler_3.4.3 pillar_1.1.0 cellranger_1.1.0 ##  scales_0.5.0 backports_1.1.2 pkgconfig_2.0.1