0

我尝试编译从数据帧中的循环获得的结果,使用 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)
4

1 回答 1

0

也许是这样的:

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)
于 2020-01-14T16:27:46.707 回答