knitr::opts_chunk$set(message = F, warning = F)
# load('~/SLATE/ceres_embed.rds')
library(readr)
fake2 = read_csv('~/SLATE/fake2.csv')
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   Code = col_character(),
##   Name = col_character(),
##   Link = col_character(),
##   Credits = col_double(),
##   Semester = col_character(),
##   Department = col_character(),
##   Description = col_character(),
##   LO = col_character()
## )
library(DT)
library(tidyverse)

datatable2 <- function(x, vars = NULL, opts = NULL, ...) {
  
  names_x <- names(x)
  if (is.null(vars)) stop("'vars' must be specified!")
  pos <- match(vars, names_x)
  if (any(map_chr(x[, pos], typeof) == "list"))
    stop("list columns are not supported in datatable2()")
  
  pos <- pos[pos <= ncol(x)] + 1
  rownames(x) <- NULL
  if (nrow(x) > 0) x <- cbind(' ' = '&oplus;', x)

  # options
  opts <- c(
    opts, 
    list(
      columnDefs = list(
        list(visible = FALSE, targets = c(0, pos)),
        list(orderable = FALSE, className = 'details-control', targets = 1),
        list(className = 'dt-left', targets = 1:3),
        list(className = 'dt-right', targets = 4:ncol(x))
      )
  ))
  
  datatable(
    x, 
    ...,
    escape = -2,
    options = opts,
    callback = JS(.callback2(x = x, pos = c(0, pos)))
  )
}

.callback2 <- function(x, pos = NULL) {
  
  part1 <- "table.column(1).nodes().to$().css({cursor: 'pointer'});"
  
  part2 <- .child_row_table2(x, pos = pos)
  
  part3 <- 
  "
   table.on('click', 'td.details-control', function() {
    var td = $(this), row = table.row(td.closest('tr'));
    if (row.child.isShown()) {
      row.child.hide();
      td.html('&oplus;');
    } else {
      row.child(format(row.data())).show();
      td.html('&ominus;');
    }
  });"
  
  paste(part1, part2, part3)
} 

.child_row_table2 <- function(x, pos = NULL) {
  
  names_x <- paste0(names(x), ":")
  text <- "
  var format = function(d) {
    text = '<div><table >' + 
  "

  for (i in seq_along(pos)) {
    text <- paste(text, glue::glue(
        "'<tr>' +
          '<td>' + '{names_x[pos[i]]}' + '</td>' +
          '<td>' + d[{pos[i]}] + '</td>' +
        '</tr>' + " ))
  }

  paste0(text,
    "'</table></div>'
      return text;};"
  )
}

kableExtra

library(kableExtra)
fake2 %>%
  select(-Link, -Description, -LO) %>% 
  head(20) %>% 
  kbl() %>%
  kable_styling()
X1 Code Name Credits Semester Department
1 ALLV221 Literary Theory 10 Autumn Department of Linguistic, Literary and Aesthetic Studies
2 AORG107 EU and International Organisations 15 Autumn Department of Administration and Organization Theory
3 AORG108 Systems of Governance and the Global Challenges of Energy and Climate 10 Spring Department of Administration and Organization Theory
4 AORG109 Systems of Governance and the Global Challenges of Energy and Climate 15 Spring Department of Administration and Organization Theory
5 AORG320B Organization Theory 10 Autumn Department of Administration and Organization Theory
6 AORG321B Quantitative Methods 10 Spring Department of Administration and Organization Theory
7 AORG322B Public Policy 10 Autumn Department of Administration and Organization Theory
8 AORG323B Qualitative Methods 10 Autumn Department of Administration and Organization Theory
9 AORG325 Discretion and paternalism 10 Spring Department of Administration and Organization Theory
10 AORG326 Political Organization and Multilevel Governance 10 Spring Department of Administration and Organization Theory
11 AORG327 Knowledge, Politics and Organization 10 Spring Department of Administration and Organization Theory
12 AORG332 Research Design 10 Spring Department of Administration and Organization Theory
13 AORG351 Master’s Thesis in Public Administration 60 Spring, Autumn Department of Administration and Organization Theory
14 ARK111 Thematic Specialization in Archaeology in the Period up to 500 BC (1) 10 Spring Department of Archaeology, History, Cultural Studies and Religion
15 ARK112 Thematic Specialization in Archaeology in the Period up to 500 BC 10 Spring Department of Archaeology, History, Cultural Studies and Religion
16 ARK113 Thematic Specialization in Archaeology in the Period up to 500 BC (3) 10 Spring Department of Archaeology, History, Cultural Studies and Religion
17 ARK122 Thematic Specialization in Archaeology in the Period Between 500 BC - 1500 AD (2) 10 Autumn Department of Archaeology, History, Cultural Studies and Religion
18 ARK123 Thematic Specialization in Archaeology in the Period Between 500 BC - 1500 AD (3) 10 Autumn Department of Archaeology, History, Cultural Studies and Religion
19 BINF100 Introduction to bioinformatics 10 Spring Department of Informatics
20 BINF200 Analysis of biological sequences and structures 10 Autumn Department of Informatics

DT

datatable2(
  x = fake2, 
  vars = c("X1", "Link", "Description", "LO"),
  opts = list(pageLength = 20)
)