1

A very simple shiny app with upload feature is working locally but not on shinyapps.io

Code :

ui <- shiny::fluidPage(
  fileInput("file", "Upload file")
)
server <- function(input, output){

}
shinyApp(ui, server)

Error occurring looks like this :

enter image description here

Link to shinyapps.io app : https://babai.shinyapps.io/stackO/

File to test on upload : http://www.mediafire.com/file/lh98smuid1mprxj/2018-05-20_02-00-10.mat/file

Have seen the following similar question ( Error deploying shiny app that uses fileInput to upload data ). But the solutions here does not work.

Soumya Boral
  • 1,191
  • 14
  • 28

1 Answers1

0

It seems, that the problem is caused by the specific encoding, which shinyapps.io doesn't recognize. The function fileInput() tries to read the file content immediately in order to wrap it to data.frame with the predefined structure, and it fails when unknown encoding or corrupted file is provided. From the other hand, shinyapps.io has restricted possibilities to setup environment and encoding.

So, instead I'd suggest an alternative way of reading files with readLines() function. In the below example the user has to provide URL, which is then being read into a reactive variable. The variable then can be analyzed or processed. The test URL from your example does not cause errors.

library(shiny)

ui <- shiny::fluidPage(
  fileInput("file", "Upload file"),              # FileInput (1)
  textOutput("content1"),                        # Text fragment (1)
  textInput("url", label = "Provide URL"),       # URL input (2)
  actionButton("download", label = "Read file"),
  textOutput("content2")                         # Text fragment (2)
)

server <- function(input, output) {
  reactive_val <- reactiveValues()

  # File content
  output$content1 <- renderText({
    if (is.null(input$file)) return(NULL)
    x <- readLines(input$file$datapath, n = 5, skipNul =T)
    enc2native(x)
  })

  # Read URL content
  observeEvent(input$download, {
    my_file <- input$url
    if (is.null(my_file)) return(NULL)
    file_content <- readLines(my_file, n = 5, skipNul =T)
    reactive_val[["content"]] <- enc2native(file_content)
  }, ignoreInit = T)

  # URL file content
  output$content2 <- renderText({
    if (is.null(reactive_val[["content"]])) return(NULL)
    reactive_val[["content"]]
  })
}
shinyApp(ui, server)
DzimitryM
  • 561
  • 1
  • 5
  • 13