ramesh
ramesh

Reputation: 1217

Shiny error: 'file' must be a character string or connection

Question #1: My ui.R code:

fileInput('file1', h5('Choose input file: (Use only tab delimited text files)'),
              accept=c('text', 'text-separated-values'))

and server.R code

inFile <- input$file1
dat<-read.table(inFile$datapath, header=TRUE, sep="\t")

when I launch shiny, I am getting, Error: 'file' must be a character string or connection. But after uploading the file, the error goes away. I'm wondering what could be the problem? I'd appreciate any pointers!

Question #2: How to suppress error message in R console, when shiny is running?

Thanks in advance

Upvotes: 5

Views: 14280

Answers (2)

Jan Stanstrup
Jan Stanstrup

Reputation: 1232

You should be reading your file inside a reactive function. For example inside a renderTable.

You then need to add

if(is.null(input$file1))     return(NULL) 

as the first thing in your reactive function.

The error is because you are trying to read a file with path NULL that is the value of input$file1 before a file is uploaded.

Update: shiny now has a function that deals more cleanly with this. You can instead add req(input$file1) at the start of your render function.

Upvotes: 8

Angel R
Angel R

Reputation: 11

You then need to add

validate(
  need(input$file1 != "", "No data has been uploaded")
)

This link will be usefull for you https://shiny.rstudio.com/articles/validation.html

Upvotes: 1

Related Questions