在Shiny中使用Source()

Ela*_*iha 21 r shiny

我有一个工作的R应用程序,我想使用Shiny在线提供.我的应用程序接收文件作为输入,因此客户端通过ui.R上传文件.server.R接收文件,然后我想调用我的应用程序.但是,当我使用source()时,myApp不知道我在server.R中收到的文件并抛出错误:找不到对象.这是server.R的代码

shinyServer(function(input, output) {

   output$contents <- renderTable({
   inFile <- input$file1
   if (is.null(inFile))
      return(NULL)
   else{
      tdata <- as.matrix(read.table(inFile$datapath))
      head(tdata, n = 2)
      source("./CODE/run_myApp.r")
   }
  })
})
Run Code Online (Sandbox Code Playgroud)

但是,myApp不包含tdata(在我当前的应用程序中需要作为输入文件).

jdh*_*son 19

要在闪亮的应用程序中使用source,您需要调用local = TRUE参数,所以在这种情况下:

shinyServer(function(input, output) {

   output$contents <- renderTable({
   inFile <- input$file1
   if (is.null(inFile))
      return(NULL)
   else{
      tdata <- as.matrix(   read.table(inFile$datapath))
      head(tdata, n = 2)
      source("./CODE/run_myApp.r", local = TRUE)
   }
  })
})
Run Code Online (Sandbox Code Playgroud)