file.copy,loadworkbook 无法在 R 闪亮的反应环境中工作,但在反应环境之外工作

file.copy, loadworkbook not working in R shiny reactive environment but working outside reactive environment

我创建了以下应用程序以从 R 中的临时文件夹中读取 excel 文件。我想在 R shiny 中保留文件格式

library(XLConnect)
library(shiny)
library(openxlsx)
library(readxl)

ui <- fluidPage(   
titlePanel("Writer App"),
sidebarLayout(sidebarPanel(fileInput(inputId = "file", label = "Read File Here", accept = c(".xlsx")), downloadLink("downloadData", "Download")), mainPanel(dataTableOutput(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, pattern = ".xlsx" )==T){data=read_excel(infile$datapath)} else 
if(grepl(infile , pattern = ".csv" )==T){data=read.csv(infile$datapath )}

#RENAME DATAFRAME WITH UNDERSCORES
names(data)<-gsub(pattern = " ", replacement = "_", x =  names(data))
return(data) })
output$table1 <- renderDataTable({     
return(datasetInput())})


 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)

下载按钮 (output$downloadData) 有一个命令,用于将包含格式的文件从临时位置复制到新文件 temp.xlsx。该文件接下来作为工作簿加载。 a New sheet Parameters1 添加到工作簿 wb2。接下来,将数据帧(df_1)写入sheet Parameters1并下载文件。

现在,output$downloadData 中的代码可以在 R 控制台中运行,但不能在 R shiny 的反应环境中运行。

loadworkbook 命令似乎有错误。我可以通过 read_excel 获取文件,然后插入数据。然而,这并不保留加载的 excel sheet 的格式。我请求有人指导我。

这对我来说没有任何错误。

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

ui <- fluidPage(   
  titlePanel("Writer App"),
  sidebarLayout(sidebarPanel(fileInput(inputId = "file", label = "Read File Here", accept = c(".xlsx")), downloadLink("downloadData", "Download")), 
                mainPanel(dataTableOutput(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 <- renderDataTable({     
    return(datasetInput())})
  
  
  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)

问题已通过使用 xlconnect 包解决。这是我使用的代码

library(XLConnect)
library(shiny)
library(readxl)

 ui <- fluidPage(   
 titlePanel("Writer App"),
 sidebarLayout(sidebarPanel(fileInput(inputId = "file", label = "Read File 
 Here", accept =c(".xlsx")), downloadButton("downloadData", "Download")),                  
 mainPanel(dataTableOutput(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, pattern = ".xlsx" )==T){
  data=read_excel(infile$datapath)
} else if(grepl(infile , pattern = ".csv" )==T)
{data=read.csv(infile$datapath )}

#RENAME DATAFRAME WITH UNDERSCORES
names(data)<-gsub(pattern = " ", replacement = "_", x =  names(data))
return(data) })
output$table1 <- renderDataTable({     
return(datasetInput())})
 output$downloadData <- downloadHandler(
 filename = function() {
  "file.xlsx"},
content = function(file) {
wdf<-getwd()      
tf<-tempdir()      
Files=list.files(path=tf, pattern=".xlsx", recursive = TRUE)[1]
newWB <- loadWorkbook(filename=paste0(tf,"/",Files),create=TRUE)
createSheet(object = newWB,name='Parameters')
writeWorksheet(newWB,data=data.frame("X"=c(1:10),"Y"= 
c(1:10)),sheet='Parameters',header=TRUE,rownames=NULL)
saveWorkbook(object = newWB, file = file)})
}
shinyApp(ui = ui, server = server)

注意:我已经对 openxlsx 进行了注释,以避免 openxlsx 和 xlconnect 之间的不兼容。另一个更改涉及删除 xlsx 包,因为这也不兼容 xlconnect