## DT Server Side
library(shiny)library(DT)library(data.table)ID=seq(1,nrow(mtcars))data=data.table(ID,mtcars)shinyApp( ui = fluidPage( title = 'Server-side processing of DataTables, with Row Selector' ,fluidRow(uiOutput('info'))
,fluidRow(DT::dataTableOutput('tbl')) ,fluidRow(textOutput('log'))
,fluidRow(uiOutput('link'))
), server = function(input, output, session) {
output$info=renderUI(h2('DT table Server Side with Row Selector'))
output$tbl = DT::renderDataTable( datatable(data ,server=TRUE ,escape=FALSE
,rownames = FALSE ,options=list( ajax = list(url = dataTableAjax(rowname=FALSE, session, data)) #must be present if server side, must have the 'data' frame used for the table ) ,callback = JS(
"table.on('click.dt', 'tr', function() { if ( $(this).hasClass('selected') ) { $(this).removeClass('selected'); } else { table.$('tr.selected').removeClass('selected'); $(this).addClass('selected'); }
Shiny.onInputChange('tbl_selected',
table.rows('.selected').data().toArray()); });")
) ) output$link=renderUI(a('Documentation',href='https://rstudio.github.io/DT/server.html')) output$log=renderText(paste0('ID selected: ',paste(input$tbl_selected[1],collapse=','))) })
library(shiny)library(data.table)library(DT)
data=data.table(mtcars)shinyApp( ui = fluidPage(
title = 'Template' ,fluidRow(uiOutput('info'))
,fluidRow(DT::dataTableOutput('tbl')) ,fluidRow(textOutput('log'))
,fluidRow(uiOutput('link'))
), server = function(input, output, session) {
output$info=renderUI(h2('DT table with Row Selector, Table Tools, Scroller, Column Selector (Client Side)')) output$tbl = DT::renderDataTable({ #data=appendCheckboxes(data,after = FALSE) datatable(data ,server=FALSE ,escape=FALSE ,extensions = c('TableTools','ColVis','Scroller') ,rownames = FALSE ,filter = "bottom" #filter must be at bottom if there is Scroller ,options=list( dom='iTC<"clear">frtS' #C<"clear">=Column Selecter S=Scroller T=TableTools ,scrollY='300px' ,scrollX=TRUE ,deferRender=FALSE ,tableTools = list(sSwfPath = copySWF('www') ,aButtons = list('copy', 'print','xls') #must have copy_csv_xls.swf in www dir ) ) ) }) output$log=renderText(paste0('Rows selected: ',paste(input$tbl_selected,collapse=','))) output$link=renderUI(a('Documentation',href='https://rstudio.github.io/DT/shiny.html')) })