我已经完成了一个程序来转换一个Excel文件,他可以完美地独立工作。我的问题是,我试图将其包含在一个 Shiny 的应用程序中以便与其他人共享。目标是上传一个与特定模板对应的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)
这是错误:
Error in if (multiple) inputTag$attribs$multiple <- "multiple" : argument is not interpretable as logical In addition: Warning message: In if (multiple) inputTag$attribs$multiple <- "multiple" : the condition has length > 1 and only the first element will be used
预先感谢您的帮助。
最佳答案
您将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'))
)
)
关于r - 上传,转换xlsx文件和下载结果 Shiny ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49359097/