首页 > 解决方案 > 为什么 R while 循环不能用闪亮的框架实现?

问题描述

我正在尝试在闪亮的仪表板上创建一个简单的文本列表 UI。为此,我制作了一个 R 函数,它需要存储每个文本输入以存储在列表中。

readtasks <- function() {

  df <- list()
  while(TRUE) {
    tasks <- readline(prompt="type your tasks: ")
    # stop reading if no text was typed in
    if (tasks == '')
      break

    # add the read data to the bottom of the list
    df <- append(df,tasks)
  }
  print(df)
}

该功能适用​​于我的需求,但当涉及到闪亮时,它会显示很多错误。我急切地想知道,这个while循环如何有效地实现闪亮。我在谷歌和 StackOverflow 上搜索了很多关于这个的内容。如果可能的话,请给我一个例子。

谢谢

我尝试用上述功能制作一个闪亮的应用程序

ui<- fluidPage(

  textInput(inputId = "text","add"),
  actionButton("button",label = "enter"),
  verbatimTextOutput("list")
)



server<- function(input,output,session){


       df<- list()
    while(TRUE) {
      tasks <- input$text
        # stop reading if no year was typed in
        if (tasks == '')
          break

      # add the read data to the bottom of the dataframe

      df <- append(df,tasks)

      output$list<- renderprint({df})


      }

}

shinyApp(ui = ui,server = server)

但它因以下错误而崩溃

Listening on http://127.0.0.1:7598
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  61: stop
  60: .getReactiveEnvironment()$currentContext
  59: getCurrentContext
  55: .subset2(x, "impl")$get
  54: $.reactivevalues
  52: server [#6]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

知道使用 while 循环执行函数真的很有帮助

再次感谢你

标签: rloopswhile-loopshiny

解决方案


推荐阅读