【问题标题】:how to refer two data frames from one reactive function in shiny如何在闪亮的一个反应函数中引用两个数据帧
【发布时间】:2018-01-24 17:18:04
【问题描述】:

我在server.r 中有以下反应函数

 test <- reactive({ 

 test_1
 test_2

})

output$table1 <- renderTable({

 test_1
})

output$table2 <- renderTable({

 test_2
})

我想将test_1 &amp; test_2 从一个反应函数引用到两个不同的输出表。

我怎样才能达到上述目的?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    引用多个数据帧的正确方法如下

    test <- reactive({ 
      test_1
      test_2
      list(df1 = test_1, df2 = test_2)
    
    })
    
    output$table1 <- renderTable({
       test()[['df1']]
    })
    output$table2 <- renderTable({
       test()[['df2']]
    })
    

    【讨论】:

      【解决方案2】:

      只有一个功能,但返回:

      list(df1=test_1, df2=test_2)
      

      然后在你的其他函数中参考这些

      test()$df1
      test()$df2
      

      不要忘记$前的括号

      【讨论】:

        【解决方案3】:

        尝试将响应式数据帧放入列表中:

        test <- reactive({ 
          test_1
          test_2
          testData <- list(test_1, test_2)
          testData ## added this
        })
        
        output$table1 <- renderTable({
          testData <- test()
          testData$test_1
        })
        output$table2 <- renderTable({
          testData <- test()      
          testData$test_2
        })
        

        它的扩展版本适用于“renderLeaflet”应用程序。

        【讨论】:

        • 我刚刚在反应函数中添加了一个需要的行来指定返回 testData。如果这不起作用,请告诉我。
        • 我仍然收到同样的错误testData not found
        • 正确,如您所说。我没有使用实际的 MWE,很高兴“将其放入列表”的方法对您有用。
        猜你喜欢
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-04
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        • 2017-02-23
        相关资源
        最近更新 更多