2016-03-06 76 views
2

我使用一個函數返回了一個n數據框的列表:根據變量的層數,n是可變的。顯示從列表輸出中提取的多個表格

我想顯示使用循環這n個表中server.R

這裏是我的代碼:

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.) 
+0

1 tableOutput只能輸出1個表。不是多個。 – GAURAV

+0

謝謝Gaurav, 但它是唯一的一個數據表。 outlist2()$ res.comparer [[i]]是一個表。 –

回答

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]] 

     }) 

     }) 

}