【问题标题】:R Shiny error with rbind to compile results from a loop : error evaluation nested too deeply: infinite recursionR Shiny 错误与 rbind 编译循环结果:错误评估嵌套太深:无限递归
【发布时间】:2020-01-14 15:55:41
【问题描述】:

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

“错误:评估嵌套太深:无限递归/选项(表达式=)?”

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

谢谢!

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)

【问题讨论】:

    标签: r shiny rbind infinite-recursion


    【解决方案1】:

    可能是这样的:

    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)
    

    【讨论】:

    • 成功了,非常感谢! :-) 我不确定 observeEvent 指令在这里使用是否有用,但它可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多