r - 从 Shiny renderDataTable 保存

标签 r datatables jquery-datatables shiny tabletools

这个问题以前有人问过R shiny dataTables with TableTools and other extensionsUse TableTools for DataTables in R Shiny for renderDataTable但我目前正在努力让 TableTools 的演示从 Shiny 的应用程序下载 csv 或 xls。该按钮似乎可以工作,并且 csv/xls 选项可用,但没有任何反应。我唯一能想到的就是将数据表版本从 1.9.4 更新到 1.10.1,但仍然没有。除非我犯了一些愚蠢的错误,否则所有其他路径都是最新的。谢谢。

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

最佳答案

在倾注了这个之后,我发现了我最初的错误。令人尴尬的是,主要问题是我试图直接从 Shiny 应用程序的 rstudio 启动中保存。从那以后我才知道这是不可能的,因为 rstudio 没有 flash。因此,最初的解决方案是简单地运行该应用程序,然后在安装了 flash 的浏览器中打开它。然后保存按钮正常工作,我可以保存过滤后的数据集。

但是,在我努力的过程中,我发现了一些其他人可能会觉得有用的简化方法。

shiny 的下一个版本将使用更新的 1.10.2 DataTables。您可以从 github page 下载当前开发版本.这允许在更新其他源文件的链接后省略 zeroclipboard 文件。下面是我发现在浏览器中使用最新的 shiny 包打开时有效的最终代码。请注意,由于 DataTables 1.10.x 已转换为驼峰命名法,因此符号略有变化。发现了旧符号的转换 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")
          )
        )
      )
    )
    )
  })
)

编辑

从 DT 0.1.5x 开始,界面随着 TableTools 的更新而改变。您现在使用 Buttons 扩展。 注意 - 这仍然 (3/29/16) 需要用户从 github 安装直到作者认为更新足以推送到 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')
    )
    )
  })
)

关于r - 从 Shiny renderDataTable 保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554068/

相关文章:

r - 如何按 dotplot 或 ggplot2 中的随机效应值(而不是截距)对随机效应进行排序

根据与输入 df 的逻辑比较返回子集

javascript - 数据表中的多个固定 header

javascript - 客户端从 IndexedDB 延迟加载到 DataTables jquery 插件

jquery - 数据表对象的对象而不是对象的数组

javascript - 无法重新初始化 JQuery DataTable

javascript - Json 到 Jquery 数据表

python - Python 中是否有等同于 R apply 函数的函数?

r - 调用序列化 R 函数时出错

jquery - Datatables 1.10 - TD 中的 HTML5 "data-order"attr 无效