r当回路内部观察功能未正确断开时

r当回路内部观察功能未正确断开时,r,while-loop,output,break,reactive,R,While Loop,Output,Break,Reactive,编辑以添加可复制代码 我正在构建一个闪亮的应用程序,在观察函数中遇到了while循环问题。程序在while循环后不显示反应值。下面是ui.r和server.r中的相关代码片段 用户界面 服务器.r library(shiny) library(stringr) predict <- function(term) { if(term == 3) table <- list() else if(term == 0) table <- c("in

编辑以添加可复制代码

我正在构建一个闪亮的应用程序,在观察函数中遇到了while循环问题。程序在while循环后不显示反应值。下面是ui.r和server.r中的相关代码片段

用户界面

服务器.r

library(shiny)
library(stringr)

predict <- function(term)
{
  if(term == 3)
    table <- list()
  else
    if(term == 0)
      table <- c("input","text","empty")
    else
      table <- c("words","were","found")
  return(table)
}

shinyServer(
  function(input, output) {
    state <- reactiveValues()
    observe({
      state$inText <- input$inText
      state$wcount <- sapply(gregexpr("[[:alpha:]]+", state$inText), function(x) sum(x > 0))

      if( state$wcount > 2)
        term.c <- 3
      else
        if( state$wcount == 2)
          term.c <- 2
        else
          if( state$wcount == 1)
            term.c <- 1
          else
            term.c <- 0
      cont <- TRUE

      while(cont == TRUE) {
        if(term.c == 3) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 2
        }
        if(term.c == 2) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 1
        } 
        if(term.c == 1) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 0
        } 
        if(term.c == 0) {
          state$predList <- c("Did", "not", "find", "term")
          break
        }
      }
    })
    output$outText <- renderPrint({ input$inText })
    output$outCount <- renderPrint({ sapply(gregexpr("[[:alpha:]]+", input$inText), function(x) sum(x > 0)) })
    output$outPred <- renderPrint({ state$predList })
  }
)
库(闪亮)
图书馆(stringr)

预测如果你加入了一个团队,帮助会更容易。现在,我们不能将它复制/粘贴到R中来测试它,看看到底发生了什么。试着简化这个例子。如果我删除
predict
表达式并用简单的东西替换它们,你的例子就可以了。是否使用断点进行调试?您是否在UI中定义了一个
textOutput(“outPred”)
?很抱歉,我已经创建了一个可重现的示例来说明我遇到的问题。在实际的程序中,没有找到任何内容的搜索将返回一个空列表。所以我创建了这个例子,如果你输入三个单词,它也会返回一个空列表。然后,两个程序执行另一个
predict
函数,该函数返回一个填充的列表,但从不显示@MrFlick@shosacoTry将
while(){}
循环为
隔离({})
。现在,您正在为输入和输出使用反应值,因此当您更新值时,您正在创建一个无限循环。非常感谢。我已经为此挣扎了两周。你的建议奏效了。
library(shiny)
library(stringr)

predict <- function(term)
{
  if(term == 3)
    table <- list()
  else
    if(term == 0)
      table <- c("input","text","empty")
    else
      table <- c("words","were","found")
  return(table)
}

shinyServer(
  function(input, output) {
    state <- reactiveValues()
    observe({
      state$inText <- input$inText
      state$wcount <- sapply(gregexpr("[[:alpha:]]+", state$inText), function(x) sum(x > 0))

      if( state$wcount > 2)
        term.c <- 3
      else
        if( state$wcount == 2)
          term.c <- 2
        else
          if( state$wcount == 1)
            term.c <- 1
          else
            term.c <- 0
      cont <- TRUE

      while(cont == TRUE) {
        if(term.c == 3) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 2
        }
        if(term.c == 2) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 1
        } 
        if(term.c == 1) {
          state$predList <- predict(term.c)
          if(length(state$predList) > 0) break
          else term.c <- 0
        } 
        if(term.c == 0) {
          state$predList <- c("Did", "not", "find", "term")
          break
        }
      }
    })
    output$outText <- renderPrint({ input$inText })
    output$outCount <- renderPrint({ sapply(gregexpr("[[:alpha:]]+", input$inText), function(x) sum(x > 0)) })
    output$outPred <- renderPrint({ state$predList })
  }
)