We have been using client-side processing in all the above examples, i.e., searching, pagination, and ordering are done using JavaScript in the web browser. When the size of the data is huge, it may be more efficient to process the data on the server. DataTables supports retrieving data from a server by sending Ajax requests, a.k.a the “server-side processing”.

1 A Shiny Example

If you have used DataTables in shiny before, you might know that shiny uses the server-side processing model for DataTables, which basically means the data is processed through R, and R will return the JSON data to the client.

Server-side processing in Shiny is enabled by the DT::renderDataTable parameter server = TRUE; it is enabled by default. There is an example in the DT package, which you can run by

shiny::runApp(system.file('examples', 'DT-shiny', package = 'DT'))

Below is the source code of the example app:

1.1 ui.R


  title = 'Use the DT package in shiny',
  h1('A Table Using Client-side Processing'),
    column(8, DT::dataTableOutput('tbl_a')),
  h1('A Table Using Server-side Processing'),
    column(8, DT::dataTableOutput('tbl_b')),

1.2 server.R


shinyServer(function(input, output, session) {
  output$tbl_a = DT::renderDataTable(iris, server = FALSE)
  output$tbl_b = DT::renderDataTable(iris)

2 A JSONP Example

Now let’s try the JSONP data source example on the DataTables website. It is fairly simple to reproduce that example here using the datatable() function with an option ajax that specifies the data URL:

employee = data.frame(
  `First name` = character(), `Last name` = character(), Position = character(),
  Office = character(), `Start date` = character(), Salary = numeric(),
  check.names = FALSE
datatable(employee, rownames = FALSE, options = list(
  ajax = list(
    serverSide = TRUE, processing = TRUE,
    url = 'https://datatables.net/examples/server_side/scripts/jsonp.php',
    dataType = 'jsonp'