2016-05-26 5 views
-1

alle.R Glänzend: For-Schleife speichert nur 1 Iteration in Liste

Ich programmiere eine Simulation App in Shiny R und ich bleibe bei den for-Schleifen.

Grundsätzlich in einem reaktiven Ich rufe eine Funktion, die durch ein paar andere Funktionen Schleifen, wie folgt aus:

Im server.R:

output.addiction <- reactive ({ 
       SimulateMultiple(input$no.simulations, vectors(), parameters(), input$S.plus, input$q, 
           input$weeks, input$d, list.output) 
     }) 

Die Funktion:

SimulateMultiple <- function (no.simulations, vectors, parameters, S.plus, q, weeks, d, list.output) { 

     for (i in 1:no.simulations) { 
         thisi <- i 

         simulation <- SimulateAddictionComponents(vectors, parameters, S.plus, q, weeks, d) # returns list "simulation" 

         df.output <- BuildOutputDataframe(weeks, simulation, vectors) # returns "df.outout" 

         output.addiction <-BuildOutputList(df.output, simulation, list.output) # returns "output.addiction" 

     } 

     return(output.addiction) 
} 

Und noch einmal, die letzte Funktion, die die Out-Put-Liste erstellt:

BuildOutputList <- function (df.output, simulation, list.output) { 
     addiction <- simulation$addiction 

     output.w.success <- list(df.output, addiction) # includes success data 
     output.addition <- c(list.output, list(output.w.success)) # adds the new data to the list 
     return(output.addition) 
} 

Ich habe viel über das Thema online gelesen, ich habe versucht, ein paar Sachen zu isolieren, ein lokales ({}) usw. einzuführen. Aber es funktioniert nie. Am Ende bekomme ich eine Liste der Länge 1.

Ich wäre für immer dankbar, wenn Sie mir helfen könnten - ich bin seit zwei Tagen hier.

+0

Initialisieren Sie 'output.addiction' außerhalb für Schleife, dann fügen Sie' [i] 'undex in dieser Zeile hinzu:' output.addiction [i] <-BuildOutputList (df.output ...) ' – zx8754

+0

Mögliches Duplikat von [Loop in R: wie die Ausgaben gespeichert werden?] (Http://stackoverflow.com/questions/9689387/loop-in-r-how-to-save-the-outputs) – zx8754

Antwort

0

Das Problem gelöst, selbst wenn ich den Code in der Funktion von

output.addition <- c(list.output, list(output.w.success)) # adds the new data to the list 
     return(output.addition) 

zu

list.output <- c(list.output, list(output.w.success)) # adds the new data to the list 
     return(list.output) 

, um nicht überschreibt die Aufgabe, jedes Mal in der Schleife bearbeitet. Immerhin - sehr einfaches und dummes Problem, aber schwer zu erkennen.