Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This shows how to add a DataTables plug-in (in this case TableTools) to your Shiny app.
library(shiny)
library(ggplot2)
# Download DataTables JS file and TableTools full package from http://datatables.net/download/
# Change the paths appropriately:
addResourcePath('datatables','/Users/yourusername/Downloads/DataTables-1.9.4/media')
addResourcePath('tabletools','/Users/yourusername/Downloads/TableTools-2.1.5/media')
runApp(list(
ui = basicPage(
h1('Diamonds DataTable with TableTools'),
# Shiny imports the DataTables JS file it ships with, but after TableTools files are imported,
# which causes an error. So we have to manually import the DT JS file.
# We do this with Shiny's singletons:
tagList( # The four core files: 3 JS files and 1 CSS file --
singleton(tags$head(tags$script(src='/datatables/js/jquery.dataTables.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='/tabletools/js/TableTools.js',type='text/javascript'))),
singleton(tags$head(tags$script(src='/tabletools/js/ZeroClipboard.js',type='text/javascript'))),
singleton(tags$head(tags$link(href='/tabletools/css/TableTools.css',rel='stylesheet',type='text/css')))
),
dataTableOutput("mytable")
),
server = function(input,output) {
output$mytable = renderDataTable({
diamonds[,1:5]
}, options = list(
"sDom" = 'T<"clear">lfrtip', # "used to position the various controls"
"oTableTools" = list(
"sSwfPath" = "/tabletools/swf/copy_csv_xls.swf",
# ^ "Define the path of the SWF to be used by TableTools
# for copy to clipboard and saving a file locally operations.
# If you are having problems with these operations, but not
# printing, this is very likely to be the issue."
# addResourcePath allows us to refer to /tabletools.
"aButtons" = list(
"copy",
"print",
list("sExtends" = "collection",
"sButtonText" = "Save",
"aButtons" = c("csv","xls")
)
)
)
)
)
}
))
@avilella
Copy link

avilella commented Jun 30, 2014

I don't see ZeroClipBoard as part of TableTools. Is it in another extension?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment