首页 > 解决方案 > 'file' 必须是字符串或连接

问题描述

我正在尝试创建一个闪亮的应用程序,它可以上传表格、运行函数并显示图表和表格。上传文件工作正常,但我无法运行该函数并输出图形和表格(我们现在只关注表格)。我收到错误:

Warning: Error in read.table: 'file' must be a character string or connection

我已经在 R 中单独运行了该函数,并且可以很好地处理所需的输出。我尝试了不同的读取函数和不同的分隔符/定界符,并在响应式 renderPlot 函数中读取函数(如上一篇文章所述。下面是我一直在处理的代码片段:

ui.R:

fileInput("file1", 
                  "Load Input File", 
                  accept = c("text/csv", "text/comma-separated-values,text/plain",".csv")
        )

服务器.R:

execute = observeEvent(input$runButton, {

    output$plot1 = renderPlot({
        inFile = input$file1
        if (is.null(inFile)) {
            print(NULL)
        }
        podr_fun_graphs(inFile$datapath)
    })

}

podr_graphs 函数:

podr_fun_graphs <- function(p) {

    df1 <- read.delim(p, sep = "\t")

    ... # From here there is some data cleaning and manipulation of df1

}

几周前类似的代码还在工作,我做了一些小改动,然后就坏了。将不胜感激任何帮助来解决这个问题。

谢谢

标签: rshiny

解决方案


问题出在你的if陈述中。你已经写了print(NULL)。但它应该是:

if (is.null(inFile)) {
     return(NULL)
   }

podr_fun_graphs(inFile$datapath)如果您不指定,R 将继续执行return


推荐阅读