tan tan - 9 days ago 7
R Question

Using an .xlsx file as an input in a Shiny App

I am working on a shiny app that requires a Excel with multiple sheets as an input. It was working well with the xlsx library and read.xlsx function. However, it returns the following error:

java.lang.IllegalArgumentException: Cell index must be >= 0


I've now switched to gdata and read.xls, but I get a file path error every time I run the model.

I wonder if anyone dealt with this problem before and if there are any simple solutions to fix this problem.

EDIT:

The following code

library(shiny)
library(xlsx)

ui <- fluidPage(
fileInput("uploadFile", "XLSX file"),
verbatimTextOutput("summary")
)

server <- function(input, output) ({

dataset<-reactive({
inFile <- input$uploadFile
dat<-read.xlsx(inFile$datapath, 1)
return(dat)
})

output$summary <- renderText({summary(dataset())})

})

shinyApp(ui, server)


Returns:

Error : package ‘rJava’ could not be loaded


If, instead I use

library(shiny)
library(readxl)

ui <- fluidPage(
fileInput("uploadFile", "XLSX file"),
verbatimTextOutput("summary")
)

server <- function(input, output) ({

dataset<-reactive({
inFile <- input$uploadFile
dat<-read_excel(inFile$datapath, sheet = 1)
return(dat)
})

output$summary <- renderText({summary(dataset())})

})

shinyApp(ui, server)


I get:

Error: Missing file extension.

Answer Source

(Using readxl)

The datapath column from input$uploadFile is a path to a temp file which has no extension. So read_excel() does not know what format it is.

Use read_xlsx() instead:

dat <- read_xlsx(inFile$datapath, sheet = 1)