[英]How to fix the error "file must be a character string or connection" when running my Shiny app in R?
[英]Shiny error: 'file' must be a character string or connection
問題 1:我的 ui.R 代碼:
fileInput('file1', h5('Choose input file: (Use only tab delimited text files)'),
accept=c('text', 'text-separated-values'))
和 server.R 代碼
inFile <- input$file1
dat<-read.table(inFile$datapath, header=TRUE, sep="\t")
當我啟動閃亮時,我得到, Error: 'file' must be a character string or connection
。 但是上傳文件后,錯誤消失了。 我想知道可能是什么問題? 我很感激任何指示!
問題 2:如何在 Shiny 運行時抑制 R 控制台中的錯誤消息?
提前致謝
您應該在反應式函數中讀取您的文件。 例如在 renderTable 內。
然后你需要添加
if(is.null(input$file1)) return(NULL)
作為反應式函數中的第一件事。
該錯誤是因為您在上傳文件之前嘗試讀取路徑為 NULL 的文件,該文件是 input$file1 的值。
更新:閃亮現在有一個功能可以更干凈地處理這個問題。 您可以改為在渲染函數的開頭添加req(input$file1)
。
然后你需要添加
validate(
need(input$file1 != "", "No data has been uploaded")
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.