Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use TableTools for DataTables in R Shiny for renderDataTable

For this question, I am using the R Shiny tutorial example found here:

http://rstudio.github.io/shiny/tutorial/#datatables

Running the code on this tutorial renders the application at the following URL

http://glimmer.rstudio.com/yihui/12_datatables/

What I would like to know is, once that data table is rendered, we can search it using the functionality built into the renderDataTable() function in R Shiny, but is it possible to download the data you have filtered to using the renderDataTable() function?

For instance, if in the data table search bar I type, "Very Good," only the records in the "cut" field which read "Very Good" are displayed. How would I then download that data set?

How would I invoke the TableTools.js copy, print, save, etc buttons into this code?

Thank you!

like image 802
Jonathan Charlton Avatar asked Apr 08 '14 17:04

Jonathan Charlton


1 Answers

You need to link to the correct library versions. Links to data.table 1.9.4 can be found at http://cdnjs.com/libraries/datatables . Links to tabletools 2.1.5 at http://cdnjs.com/libraries/datatables-tabletools

library(shiny)
library(ggplot2)
runApp(
  list(ui = basicPage(
    h1('Diamonds DataTable with TableTools'),
    tagList(
      singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/jquery.dataTables.min.js',type='text/javascript'))),
      singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/js/TableTools.min.js',type='text/javascript'))),
      singleton(tags$head(tags$script(src='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/js/ZeroClipboard.min.js',type='text/javascript'))),
      singleton(tags$head(tags$link(href='//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/css/TableTools.min.css',rel='stylesheet',type='text/css'))),
      singleton(tags$script(HTML("if (window.innerHeight < 400) alert('Screen too small');")))
    ),
    dataTableOutput("mytable")
  )
  ,server = function(input, output) {
    output$mytable = renderDataTable({
      diamonds[,1:6]
    }, options = list(
      "sDom" = 'T<"clear">lfrtip',
      "oTableTools" = list(
        "sSwfPath" = "//cdnjs.cloudflare.com/ajax/libs/datatables-tabletools/2.1.5/swf/copy_csv_xls.swf",
        "aButtons" = list(
          "copy",
          "print",
          list("sExtends" = "collection",
               "sButtonText" = "Save",
               "aButtons" = c("csv","xls")
          )
        )
      )
    )
    )
  })
)

enter image description here

like image 149
jdharrison Avatar answered Oct 21 '22 05:10

jdharrison