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”.
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
::runApp(system.file('examples', 'DT-shiny', package = 'DT')) shiny
Below is the source code of the example app:
library(shiny)
shinyUI(fluidPage(
title = 'Use the DT package in shiny',
h1('A Table Using Client-side Processing'),
fluidRow(
column(2),
column(8, DT::dataTableOutput('tbl_a')),
column(2)
),h1('A Table Using Server-side Processing'),
fluidRow(
column(2),
column(8, DT::dataTableOutput('tbl_b')),
column(2)
) ))
library(shiny)
library(DT)
shinyServer(function(input, output, session) {
$tbl_a = DT::renderDataTable(iris, server = FALSE)
output$tbl_b = DT::renderDataTable(iris)
output })
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:
= data.frame(
employee `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'
) ))