Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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

like image 693
ramesh Avatar asked Jul 09 '14 07:07

ramesh


2 Answers

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.

like image 81
Jan Stanstrup Avatar answered Nov 11 '22 17:11

Jan Stanstrup


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

like image 1
Angel R Avatar answered Nov 11 '22 19:11

Angel R