r语言 - 上传,转换xlsx文件和下载结果闪亮



我已经做了一个程序来转换一个excel文件,他可以独立地完美地工作。我遇到的问题是我试图将其包含在一个闪亮的应用程序中,以便与其他人共享。目标是上传与特定模板对应的 excel 文件并下载转换结果。

我收到一个错误,但我不明白。

这是我的代码:

library(shiny)
library(readxl)
library(stringr)
transformFile <- function(df1, df2){
for(i in seq(1, nrow(df1))){
element1 <- as.character(df1$Operations[i]);
for(j in seq(1, nrow(df2))){
element2 <- as.character(df2$MotsARechercher[j]);
if(str_detect(element1, ignore.case(element2))){
df1$TypeOperations[i] <- as.character(df2$TypeOperations[j]);
}
}
}
return(df1)
}
ui <- fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"),
downloadButton('file2', "Download modified file")
)
),
mainPanel(
tableOutput('contents'))
)
)

server <- function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,
paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
})
output$downloadData <- downloadHandler(
filename = function() {
paste("comptesAvecLibellesOpe.xlsx")
},
content = function(file) {
infile <- input$file1
df1 <-  read_excel(paste(infile$datapath, ".xlsx", sep = ""), 1)
df2 <-  read_excel(paste(infile$datapath, ".xlsx", sep = ""), 2)
write.csv(transformFile(df1, df2), file, row.names = FALSE)
}
)
}
shinyApp(ui, server)

这是错误:

如果(多个)输入标签$attribs$multi<-"多个"中的错误: 参数不能解释为逻辑 另外: 警告消息: 在if(多个)输入Tag$attribs$multi<- "multiple"中: 条件的长度> 1,并且仅使用第一个元素

提前感谢您的帮助。

您将downloadButton()放在 UI 定义中fileInput()内。

fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"),
downloadButton('file2', "Download modified file")
)

这修复了您的错误

ui <- fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),
downloadButton('file2', "Download modified file")
),
mainPanel(
tableOutput('contents'))
)
)

最新更新