Use TableTools for DataTables in R Shiny for renderDataTable

℡╲_俬逩灬. 提交于 2019-12-03 13:12:04

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/22944607/use-tabletools-for-datatables-in-r-shiny-for-renderdatatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!