显示从列表输出中提取的多个表

时间:2016-03-06 21:14:03

标签: r shiny

我使用一个函数返回一个n数据帧的列表:n是变量,取决于变量的级别数。

我想在server.R

中使用循环显示这n个表

这是我的代码:

 outlist2 <- reactive(label="toto", ({

     if(is.null(input$datafile)){return()}
     if(is.null(input$varinteret)
        || is.null(input$vartemps)
        #           ||is.null(input$apparie)
                   ||is.null(input$tempsrefouinon)
                   ||is.null(input$prodrefouinon)
        #           ||is.null(input$checkprod)
        #           ||is.null(input$checkprodref)
     )
     {return()}
     else
     {
       data<-filedata()
       res.comparer<-compareT0parproduit(data=data,y=input$varinteret,group=input$varprod,TemoinNametemps=input$checktempsref, group2 = input$vartemps)
     }  
 })) 
   # 

 nblevels<-reactiveValues(filedata()[,input$varprod])
 for (i in 1:nblevels){
    output$uicomparetempsT0(i) <- shiny::renderTable({ 
      outlist2()$res.comparer[[i]]

和ui.R

shiny::tableOutput("uicomparetempsT0")

我的错误消息

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    44: .getReactiveEnvironment()$currentContext
    43: .dependents$register
    42: filedata
    41: reactiveValues
    40: server [C:\Users\itm\Desktop\Documents\appli Clarins test/server.R#454]
     1: shiny::runApp
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

1 个答案:

答案 0 :(得分:0)

好, 我试过这段代码: 灵感来自此链接:https://gist.github.com/wch/5436415/

   observe(  
     outlist2 <- reactive(label="toto", ({

         if(is.null(input$datafile)){return()}
         if(is.null(input$varinteret)
            && is.null(input$vartemps)
                       && is.null(input$apparie)
                       && s.null(input$tempsrefouinon)
                       &&is.null(input$prodrefouinon)
                       &&is.null(input$checkprod)
                       &&is.null(input$checkprodref)
         )
         {return()}
         else
         {
           data<-filedata()
           res.comparer<-compareT0parproduit(data=data,y=input$varinteret,group=input$varprod,TemoinNametemps=input$checktempsref, group2 = input$vartemps)
         # res.comparer<-list(data(), data())
           }  
     })) 
       # 

)


     nblevels<-reactive({
       if(is.null(input$datafile)){return()}
       if((is.null(input$varprod))&&(is.null(input$vartemps))) {return()}
       data<-filedata()
      res<-nlevels(data[,input$varprod])

     })



     output$plots <- renderUI({
       if(is.null(input$datafile)){return()}
       if((is.null(input$varprod))&&(is.null(input$vartemps))) {return()}
       #
      else {
       # table_output_list <- lapply(1:nlevels(data()[,input$varprod]), function(i) {
        table_output_list <- lapply(1:nblevels, function(i) {
         plotname <- paste("table", i)
         tableOutput(plotname)
       })

       # Convert the list to a tagList - this is necessary for the list of items
       # to display properly.
       do.call(tagList, table_output_list)
      }
        })

          for (i in 1:nblevels)
      {
       # Need local so that each item gets its own number. Without it, the value
       # of i in the renderPlot() will be the same across all instances, because
       # of when the expression is evaluated.
       local({
         my_i <- i
         plotname <- paste("table", my_i, sep="")

         output[[plotname]] <- renderTable({
           res2<-outlist2()
           res2$res.comparer[[my_i]]

         })

       })

}