使用 rbind 编译循环结果的 R Shiny 错误:错误评估嵌套太深:无限递归

R Shiny error with rbind to compile results from a loop : error evaluation nested too deeply: infinite recursion

我尝试编译从数据帧中的循环获得的结果,使用 rbind 将每次迭代的结果添加到数据帧(这里,这是一个简化的示例)。该代码在 R 中没有问题,但在 R shiny 中它会导致无限递归问题:

"error: evaluation nested too deeply: infinite recursion / options(expressions=)?"

我尝试了几种解决方案并在论坛中进行了搜索,但未能找到完全相同的问题。如果有人可以帮助我,那就太好了!

谢谢!

library(shiny)

# Define the ui
ui <- fluidPage(

  # Sidebar with a slider input 
  sidebarLayout(
    sidebarPanel(
       sliderInput("add",
                  "Number to add:",
                  min = 2,
                  max = 20,
                  value = 10)
    ),

    mainPanel(
      tableOutput("view")
    )
  )
)


# Define the server code
server <- function(input, output, session){

  results = reactive({data.frame(i=NA, A=NA)[-1,]})

  for(j in 1:10){

      res_j   =  reactive({data.frame(i=j, A=j+input$add)})
      results =  reactive({rbind(results(), res_j())})
  }

  output$view <-renderTable(results())

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

可能是这样的:

library(shiny)

# Define the ui
ui <- fluidPage(

  # Sidebar with a slider input 
  sidebarLayout(
    sidebarPanel(
      sliderInput("add",
                  "Number to add:",
                  min = 2,
                  max = 20,
                  value = 10)
    ),

    mainPanel(
      tableOutput("view")
    )
  )
)


# Define the server code
server <- function(input, output, session){

  results = reactiveValues()

  observeEvent(input$add, {
    results$df=data.frame(i=NA, A=NA)[-1,]
    for(j in 1:10){
      res_j   = data.frame(i=j, A=j+input$add)
      results$df =  rbind(results$df, res_j)
    }
  })

  output$view <-renderTable(results$df)

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)