r - file.copy,loadworkbook 不在 R 闪亮的反应环境中工作,但在反应环境之外工作
问题描述
我创建了以下应用程序来从 R 中的 temp 文件夹中读取一个 excel 文件。我想在 R 中保留文件格式闪亮
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。该文件接下来作为工作簿加载。将新工作表 Parameters1 添加到工作簿 wb2。接下来,将数据框(df_1)写入工作表 Parameters1 并下载文件。
现在, output$downloadData 中的代码在 R 控制台中工作,但在 R Shiny 的反应式环境中不起作用。
loadworkbook 命令似乎有错误。我可以通过 read_excel 获取文件,然后插入数据。然而,这并不保留加载的 Excel 工作表的格式。我请求有人在这方面指导我。
解决方案
这对我有用,没有任何错误。
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)
推荐阅读
- python - 主机上的 Kafka 消费者未访问在 docker 中运行的 Kafka 生产者的消息
- reactjs - 如何使自动完成 unsplash 搜索栏克隆
- html - 媒体查询将 flex-direction 设置为列后,div 消失,可能是因为 flex 属性
- png - 从 PNG 到 JPG 的缩略图转换会导致全白图像
- authentication - octobercms 更改登录页面的样式
- math - 为什么 3log8(n) 是 3log8(n) + log2log2log2n 中的主导词?
- java - 如何获取 Spring Boot 默认的 Kafka 消费者对象
- python - sympy 上的特征值给出分段值
- c# - 如何实现 NUnit 的 NUnit.Engine.ITestEventListener
- python - 如何对来自不同特征工程过程的特征进行标准化和规范化?