rshinydt

DT::datatable display


in the following code, the column names disappear when the line "container=..." is uncommented.

require(DT)

DT::datatable(cars[1:5,]
    #, container=htmltools::tags$table(class="display")
)

Looking at the Page Source in the browser, the difference is the following. Container commented:

"container": "<table>\n  <thead>\n    <tr>\n      <th>speed</th>\n      <th>dist</th>\n    </tr>\n  </thead>\n</table>",

Container not commented:

"container": "<table class=\"display\"></table>",

Any idea how they can both be made to work?


Solution

  • It looks like the column names get removed if you change the container option. If you look at the code of the function, available here, you can see line 75 what happens when you don't set a container:

      if (missing(container)) container = tags$table(
        id = id,
        tags$thead(tags$tr(lapply(escapeColNames(colnames, escape), tags$th)))
      )
    

    When the container option is missing, a default container containing the headers is generated. The escapeColNames function is defined later on, it is just used to clean up the header names.

    When the container option is set, this code is not run and the container is just what you give in the option, so you have to add the column names yourself.

    You could for example do (without any escaping):

     DT::datatable(cars[1:5,],
        container=htmltools::tags$table(class="display",
              tags$thead(tags$tr(lapply(as.list(colnames(cars)), tags$th))))
    )