Yihui Xie, RStudio
2016/01/31 @ Shiny Developer Conference
A JavaScript library for interaction with HTML tables http://datatables.net
Basic usage (in JavaScript):
// this returns a DataTables API instance
$('#foo').DataTable({
pageLength: 5,
autoWidth: true
})
inst/htmlwidgets directory (JS/CSS dependencies)
htmlwidgets::createWidget()
HTMLWidgets.widget({
name: "FOO",
type: "output",
initialize: function(el, width, height) {
// initialize the element
},
renderValue: function(el, data, instance) {
// render the data in el, e.g.
// $(el).DataTable(data.options);
}
})
DT::datatable()DataTables options
{
serverSide: true,
ajax: {
url: YOUR_URL
}
}
where YOUR_URL is a URL that accepts an HTTP POST request and returns JSON data in the responsestart=0&length=10 should return the rows from 1 to 10 (note JavaScript indexes from 0), which is something like x[1:10, ] in the R syntaxthe returned JSON data is like this:
{
draw: 7,
recordsTotal: 100,
recordsFiltered: 3,
data: [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
],
...
}DT::dataTableOutput()
htmlwidgets::widgetOutput()DT::renderDataTable()
htmlwidgets::renderWidget()renderDataTable(server = TRUE))session$registerDataObj(id, data, filterFun), where session is the session object from the server function of a Shiny app (recall function(input, output, session))
session/2ca9bd983aa0c4ba9989f50ef34842fc/dataobj/id?w=filterFun = function(data, req) { ... }, where req is the POST request that contains the querying parameters
URLdecode(rawToChar(req$rook.input$read()))
httpuv::decodeURIComponent() instead of URLdecode() in base Rshiny::parseQueryString()x[1:10, ]grep(keyword, x)sort(x)Shiny.setInputValue(id, data)table.search('keyword')table.rows().indexes()e.g. every time the table is drawn, tell Shiny the current row indexes
table.on('draw', function(e) {
var cur_rows = table.rows({
page: 'current'
}).indexes();
Shiny.setInputValue('foo_current_rows', cur_rows);
})input$foo_current_rowsOn the R side:
session$sendCustomMessage(name, data)On the JS side:
Shiny.addCustomMessageHandler(name, function(data) {
// whatever you want to do with `data`
// e.g. call DataTables API
// table.row.add(data).draw()
})session$registerDataObj()Shiny.setInputValue()session$sendCustomMessage()Shiny.addCustomMessageHandler()
from the user’s perspective

from the developer’s perspective
