首页 > 解决方案 > 如何在 Shiny 中使用 DataTable Extensions 更改下载文件中的名称?

问题描述

我创建了一个闪亮的应用程序,我可以在其中下载各种文件格式(pdf、excel、csv)的表格。但是,我发现它们中的每一个都具有与我的 Shiny 应用程序相同的标题(“这是我在 Shiny 中的表”)。

我使用 DataTable 中的这个扩展

有谁知道我是否可以从下载的文件中删除该标题?

这就是我的应用程序的外观。 应用程序

这些是下载的文件(excel和pdf) 擅长

pdf

我的代码:

library(shiny)
library(DT)

ui <- fluidPage(
  
  # Application title
  titlePanel("This is my table in Shiny")
  
  , mainPanel(
    DT::dataTableOutput("fancyTable")
  ) 
  
) 

server <- function(input, output) {
  
  output$fancyTable <- DT::renderDataTable(
    datatable( data = mtcars
               , extensions = 'Buttons'
               , options = list( 
                 dom = "Blfrtip"
                 , buttons = 
                   list("copy", list(
                     extend = "collection"
                     , buttons = c("csv", "excel", "pdf")
                     , text = "Download"
                   ) ) 
                 
                
                 , lengthMenu = list( c(10, 20, -1) 
                                      , c(10, 20, "All")
                 ) 
                 , pageLength = 10
                 
                 
               ) 
               
    ) 
  )
} 

# Run the application 
shinyApp(ui = ui, server = server)

提前致谢

问候

标签: rshinydatatabledtdatatable-buttons

解决方案


尝试了很多东西并搜索了其他帖子......我找到了解决方案!

我需要将每个选项放入一个列表中,以便能够为每个选项添加“标题”参数。

library(shiny)
library(DT)

ui <- fluidPage(
  
  # Application title
  titlePanel("This is my table in Shiny")
  
  , mainPanel(
    DT::dataTableOutput("fancyTable")
  ) 
  
) 

server <- function(input, output) {
  
  output$fancyTable <- DT::renderDataTable(
    datatable( data = mtcars
               , extensions = 'Buttons'
               , options = list( 
                 dom = "Blfrtip", 
                 buttons = 
                   list("copy", list(
                     extend = "collection", 
                     buttons = list(
                       list(extend = "csv", title = "MY TITLE"),
                       list(extend = "excel", title = "MY TITLE"),
                       list(extend = "pdf", title = "MY TITLE")),
                     text = "Download"
                   )),
                 
                 lengthMenu = list( c(10, 20, -1) 
                                      , c(10, 20, "All")
                 ),
                 pageLength = 10
                 
                 
               ) 
               
    ) 
  )
} 

# Run the application 
shinyApp(ui = ui, server = server)

在这里你可以看到新的标题!

图片


推荐阅读