R Shiny中使用DataTable按钮扩展动态命名下载文件名

3

我有:


library(shiny)
library(DT)

ui <- fluidPage(
    h2("Explorer"),

    tabPanel(h3("Inspector"),
             p("Overview of data for a particular sample."),
             selectInput(inputId = "sample",
                         label = h3("Select sample"),
                         selectize = TRUE,
                         choices = names(vcf_tibbles)),
             dataTableOutput("sample_inspector")
            )
    )

server <- function(input, output) {
  output$sample_inspector <- DT::renderDataTable(

      sample_overview(sample_id = input$sample, vcf_tibbles = vcf_tibbles),
      rownames = FALSE,
      extensions = 'Buttons',
      options = list(paging = FALSE,
             dom = 'Bfrtip',
             buttons = list( list(extend = 'csv',   filename =  paste("snp", input$sample, sep = "-")),
                     list(extend = 'excel', filename =  paste("snp", input$sample, sep = "-"))))
      )
}


一切都运行良好,我选择一个样本后表格相应地更新。如果我点击CSV或Excel,相应的数据会下载。然而,文件名总是错误的
似乎数据表的内容正在更新,但按钮没有考虑到input$sample。有没有办法使按钮中的文件名参数也具有反应性?
我尝试让名称成为函数调用结果,但也无法使其工作。
谢谢!

你在server部分开头写了sammple_overview(两个m),这是故意的还是错误? - bretauv
谢谢 @bretauv - 这确实是代码示例中的一个打字错误。(与问题无关)。已修复。 - Yannick Wurm
两个问题:您能提供vcf_tibbles的示例吗?什么是sample_overview函数? - bretauv
1个回答

5

这是如何工作的:

server <- function(input, output) {
  output$sample_inspector <- DT::renderDataTable(
    iris,
    rownames = FALSE,
    extensions = 'Buttons',
    options = exprToFunction(
      list(paging = FALSE,
           dom = 'Bfrtip',
           buttons = list( 
             list(extend = 'csv',   filename =  paste("snp", input$sample, sep = "-")),
             list(extend = 'excel', filename =  paste("snp", input$sample, sep = "-"))))
    )
  )
}

你如何知道有哪些选项可用?我阅读了 https://datatables.net/reference/option/buttons.buttons.extend,但没有提到 filename - LMc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接