lundi 19 mars 2018

Upload, transform xlsx file & download result shiny

I've done a program to transform an excel file which his perfectly working independantly. The problem I have is that I'm trying to include it in a shiny app in order to share with an other person. The goal is to upload an excel file which coresponds to a specific template and download the result of the transformation.

I get an error and I don't understand it.

Here is my code:

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)

And this is the error:

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

Thank you in advance for your help.

Aucun commentaire:

Enregistrer un commentaire