从另一个函数访问从 read.csv 加载的数据框 - R Shiny

Accessing a data frame loaded from read.csv from another function - R Shiny

我无法将数据从我从 csv 文件读取的矩阵传递到另一个函数。

我尝试了解决方案 here,创建辅助数据函数并使用 data()$loadedMat 调用矩阵。

我正在

错误:'closure' 类型的对象不是子集

我有:

 shinyServer(function(input, output, session) {

   data <- reactive(function (){

       inFile <- input$file1

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

       loadedFile <- read.csv(inFile$datapath, 
                              header=FALSE, 
                              sep=input$sep, 
                              quote=input$quote)

       loadedDf <- loadedFile[2:nrow(loadedFile), ]

       #figure out how to access loadedMat from another function
       loadedMat <- as.matrix(loadedDf)


     })

     output$result <- renderText({

          input$goButton 
          x <- someFunction(data()$loadedMat)
     })
 })

反应函数 data() 不是 "contain" 矩阵 loadedMat,它 loadedMat

因此您可以像 x <- someFunction(data())

一样访问它

您 link 的回答突出显示了您将如何从 data.frame 访问列。

要访问 data.frame 的列,您需要 data$column。这同样适用于反应性数据帧:data()$column


重申@alistaire 的评论:您可能应该为您的数据选择一个不同于 data 的名称(因为 data() 已经是 R 中的一个函数)