【问题标题】:R Shiny: For loop only saves 1 iteration in listR Shiny:For循环仅在列表中保存1次迭代
【发布时间】:2016-05-26 11:11:27
【问题描述】:

每个人。

我正在 Shiny R 中编写一个模拟应用程序,但我被困在 for 循环中。

基本上,在反应式中,我正在调用一个循环遍历其他几个函数的函数,如下所示:

在server.R中:

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

功能:

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)
}

同样,最后一个创建输出列表的函数:

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)
}

我在网上阅读了很多关于这个问题的信息,我试图隔离一些东西,引入一个本地({})等。但它从来没有奏效。最后,我得到一个长度为 1 的列表。

如果您能帮助我,我将永远感激不尽 - 我已经为此工作了两天。

【问题讨论】:

  • 在for循环外初始化output.addiction然后在这一行添加[i]undex:output.addiction[i] &lt;-BuildOutputList(df.output...)

标签: r shiny


【解决方案1】:

当我在函数中编辑代码时问题自行解决了

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

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

以免在循环中每次都覆盖对象。毕竟 - 非常简单和愚蠢的问题,但很难发现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多