r - 上传,转换xlsx文件和下载结果 Shiny

标签 r if-statement error-handling upload shiny

我已经完成了一个程序来转换一个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/

相关文章:

python-3.x - Python 3.6 OpenCV实时捕获和处理

sql - 没有存储过程的错误返回

r - .Rprofile 未在 cron 下调用

r - 访问 R 中列表的第一行

java - 嗨,我试图让我的 if 语句等于多个数字

Javascript:在未定义/空迭代器上捕获 for 循环的最佳方法?

r - R 中的 react 性问题 - 您尝试做一些只能从 react 性消费者内部完成的事情

R构造文档术语矩阵如何匹配其值由空格分隔的短语组成的字典

c++ - 修复用于生成 token 的循环结构

sql - 类型 'System.Data.SqlClient.SqlConnection'的错误值无法转换为 'String'