r Shiny DataTable扩展按钮以下载使用格式的完整Excel文件

发布于 2025-02-12 18:22:24 字数 1982 浏览 0 评论 0原文

我在R Shiny中创建了以下应用程序,并具有两个下载选项。一个是常规的下载手选项 第二个是使用r d dataTable按钮扩展名,该应用程序的构造如下如下:

library(shiny)
library(openxlsx)
library(readxl)
library(DT)

ui <- fluidPage(   
titlePanel("Writer App"),
sidebarLayout(sidebarPanel(fileInput(inputId = "file", label = "Read File Here", accept = 
 c(".xlsx")), downloadLink("downloadData", "Download")), 
            mainPanel(DTOutput(outputId = "table1"))))



 server <- function(input, output) {
 datasetInput <- reactive({
 infile<- input$file
 if (is.null(infile))
  return(NULL)   
 #READ .XLSX AND .CSV FILES
 if(grepl(infile$datapath, pattern = ".xlsx" )){data=read_excel(infile$datapath)} else 
  if(grepl(infile$datapath , pattern = ".csv" )){data=read.csv(infile$datapath )}

 #RENAME DATAFRAME WITH UNDERSCORES
 names(data)<-gsub(pattern = " ", replacement = "_", x =  names(data))
 return(data) 
})

output$table1 <- renderDT({    

 datasetInput<-datasetInput()
  datatable(datasetInput,extensions = c('Buttons', 'Scroller'), 
          options = list(
            dom = 'tB',
            deferRender = TRUE,
            scrollY = 400,
            scroller = TRUE,
            buttons = list(list(extend = 'excel'))) )





})


   output$downloadData <- downloadHandler(
   filename = function() {
   paste("data-", Sys.Date(), ".xlsx", sep="")},
   content = function(file) {
   tf<-tempdir()
   Files=list.files(path=tf, pattern=".xlsx", recursive = TRUE)[1]    
   file.copy(from =paste0(tf, "/",Files ), to= "temp_1.xlsx")     
   wb2 <- loadWorkbook(file = "temp_1.xlsx")
   df_1<-data.frame("DF"= c(1:3))      
   addWorksheet(wb = wb2,sheetName =  "Parameters1")
   writeData(wb2, "Parameters1", df_1, startCol = 1, startRow = 2, rowNames = TRUE)
   saveWorkbook(wb2, file)
  })}

 shinyApp(ui = ui, server = server)

下载处理程序保留了完整格式的Excel Shete,将新的表格附加到工作簿上,并使用新表下载Excel Workbook 。是否可以使用DataTable下面的下载按钮进行操作 - 保留原始Excel的格式,添加新表并下载。

我要求有人指导我。我找不到解决方案

I have created the following app in R shiny with two download options. One is a conventional downloadhandler option
The second is using An R datatable button extension The App is structured as follows

library(shiny)
library(openxlsx)
library(readxl)
library(DT)

ui <- fluidPage(   
titlePanel("Writer App"),
sidebarLayout(sidebarPanel(fileInput(inputId = "file", label = "Read File Here", accept = 
 c(".xlsx")), downloadLink("downloadData", "Download")), 
            mainPanel(DTOutput(outputId = "table1"))))



 server <- function(input, output) {
 datasetInput <- reactive({
 infile<- input$file
 if (is.null(infile))
  return(NULL)   
 #READ .XLSX AND .CSV FILES
 if(grepl(infile$datapath, pattern = ".xlsx" )){data=read_excel(infile$datapath)} else 
  if(grepl(infile$datapath , pattern = ".csv" )){data=read.csv(infile$datapath )}

 #RENAME DATAFRAME WITH UNDERSCORES
 names(data)<-gsub(pattern = " ", replacement = "_", x =  names(data))
 return(data) 
})

output$table1 <- renderDT({    

 datasetInput<-datasetInput()
  datatable(datasetInput,extensions = c('Buttons', 'Scroller'), 
          options = list(
            dom = 'tB',
            deferRender = TRUE,
            scrollY = 400,
            scroller = TRUE,
            buttons = list(list(extend = 'excel'))) )





})


   output$downloadData <- downloadHandler(
   filename = function() {
   paste("data-", Sys.Date(), ".xlsx", sep="")},
   content = function(file) {
   tf<-tempdir()
   Files=list.files(path=tf, pattern=".xlsx", recursive = TRUE)[1]    
   file.copy(from =paste0(tf, "/",Files ), to= "temp_1.xlsx")     
   wb2 <- loadWorkbook(file = "temp_1.xlsx")
   df_1<-data.frame("DF"= c(1:3))      
   addWorksheet(wb = wb2,sheetName =  "Parameters1")
   writeData(wb2, "Parameters1", df_1, startCol = 1, startRow = 2, rowNames = TRUE)
   saveWorkbook(wb2, file)
  })}

 shinyApp(ui = ui, server = server)

When we load an excel file, The download handler retains the excel shete with its format intact, appends a new sheet to the workbook and downloads the excel workbook with a new sheet. Is it possible to do the same with the download button below the datatable- retain the format of the original excel, add a new sheet and download.

I request someone to guide me. I am unable to find a solution for this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文