3

This question has been asked previously R shiny dataTables with TableTools and other extensions and Use TableTools for DataTables in R Shiny for renderDataTable but I am currently struggling getting this demonstration of TableTools to download the csv or xls from a shiny app. The button appears to work and the csv/xls option is available but nothing happens. The only thing I could think of was updating the datatables version from 1.9.4 to 1.10.1 but still nothing. All the other paths are up to date unless I am making some silly mistake. Thank you.

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")
          )
        )
      )
    )
    )
  })
)
4

1 回答 1

7

After pouring over this, I have discovered my initial error. Embarrassingly, the primary issue was that I was trying to save directly from the rstudio launch of the shiny app. I have since learned that this is impossible because rstudio does not have flash. As such, the initial solution was to simply run the app and then open it in a browser with flash installed. The save button then works appropriately and I can save a filtered dataset.

However, during my efforts I have discovered some simplification that others may find useful.

The next version of shiny will use the more recent 1.10.2 DataTables. You can download the current dev version from the github page. This allows one to omit the zeroclipboard file after updating the links for the other source files. Below is the final code I have found works when opened in a browser with the most recent shiny package. Please note, the slight change in notation as the DataTables 1.10.x has converted to camelCase notation. Coversions from the old notation are found here.

library(shiny)
library(ggplot2)
runApp(
  list(ui = basicPage(
    h1('Diamonds DataTable with TableTools'),
    tagList(
      singleton(tags$head(tags$script(src='//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js',type='text/javascript'))),
      singleton(tags$head(tags$script(src='//cdn.datatables.net/tabletools/2.2.2/js/dataTables.tableTools.min.js',type='text/javascript'))),
      singleton(tags$head(tags$link(href='//cdn.datatables.net/tabletools/2.2.2/css/dataTables.tableTools.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(
      "dom" = '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")
          )
        )
      )
    )
    )
  })
)

EDIT

As of DT 0.1.5x the interface has changed following the TableTools updates. You now use the Buttons extension. Note - this still (3/29/16) requires the user to install from github until the author deems the updates sufficient for pushing to CRAN.

library(shiny)
library(DT)
library(ggplot2)

runApp(
  list(ui = basicPage(
    h1('Diamonds DataTable with DT'),
    dataTableOutput("mytable")
  )
  ,server = function(input, output) {
    output$mytable = renderDataTable({
      diamonds[,1:6]
    }, 
    extensions = 'Buttons',
    options = list(
      "dom" = 'T<"clear">lBfrtip',
      buttons = list('copy', 'csv', 'excel', 'pdf', 'print')
    )
    )
  })
)
于 2014-09-02T13:46:03.573 回答