使用r shiny中的动作按钮下载完整格式的excel文件

发布于 2025-02-13 13:56:33 字数 1017 浏览 1 评论 0原文

我创建了以下闪亮的应用程序

library(shiny)
library(shinyjs)
library(openxlsx)

,一旦导入软件包,我们就会

ui <- fluidPage(
fileInput(inputId = "file", label = "Read File Here", accept = c(".xlsx")),
useShinyjs(), actionButton("showtxt", "Show/Download File"))


server <- function(input, output, session){
server <- function(input, output, session){
observeEvent(input$showtxt,{
tf<-tempdir()
infile<- input$file
wb2 <-openxlsx::loadWorkbook(file = infile$datapath)
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.xlsx") })
# Call Onclick
 onclick("showtxt", runjs("window.open('test.xlsx')"))}
  shinyApp(ui=ui,server=server) 

通过创建Excel Workbook上传的克隆/副本来创建UI和服务器。按下下载时,新的Excel工作簿将具有原始工作簿的所有格式以及添加了新的表格。 该新工作簿保存在包含脚本的文件夹中。默认情况下,我该如何将其转到笔记本电脑下载文件夹。 注意:我已经使用下载处理程序尝试了一下,但是由于某些原因,我想使用一个动作按钮。 我要求某人看看并在这里引导我。先感谢您。

I have created the following shiny App

library(shiny)
library(shinyjs)
library(openxlsx)

Once we import the packages, we create the ui and server as follows

ui <- fluidPage(
fileInput(inputId = "file", label = "Read File Here", accept = c(".xlsx")),
useShinyjs(), actionButton("showtxt", "Show/Download File"))


server <- function(input, output, session){
server <- function(input, output, session){
observeEvent(input$showtxt,{
tf<-tempdir()
infile<- input$file
wb2 <-openxlsx::loadWorkbook(file = infile$datapath)
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.xlsx") })
# Call Onclick
 onclick("showtxt", runjs("window.open('test.xlsx')"))}
  shinyApp(ui=ui,server=server) 

The code works by creating a clone/ copy of the excel workbook uploaded. When the download is pressed, the new excel workbook will have all the formats of the original workbook along with a new sheet added.
This new workbook is saved in the folder containing the script. How can I make it go to my laptops download folder by default.
Note: I have tried this with a download handler but for some reasons, I would like to use an action button.
I request someone to take a look and guide me here. thank you in advance.

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

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

发布评论

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