R Shiny:无限递归

Rya*_*n S 3 recursion r shiny

我正在开发一个 Shiny 应用程序,旨在猜测用户输入的文本片段中的下一个单词。为此,我加载三个数据帧,其中包含对下一个单词的可能猜测,并使用 grepl 查找与用户输入文本末尾的三元组或二元组匹配。我在“常规”R 中构建了这个,没有错误或问题,但是当将其转换为 Shiny 时,我收到此错误:

错误:求值嵌套太深:无限递归/选项(表达式=)?

正如其他堆栈溢出帖子所建议的那样,我摆弄了选项(表达式=)值,但仍然遇到错误。没有太多其他信息来指导我,并且注意到它没有问题,任何人都可以看到我哪里出了问题吗?提前致谢!

作为背景,我正在运行:Win 7,64 位操作系统 | R v3.1.1 | RStudio v0.98.944

ui.R

library(shiny)
shinyUI(fluidPage(
   titlePanel("Capstone: Word Guesser"),
      fluidRow(
         column(12, 
            mainPanel(
               h4('Sentence Fragment'),
               p('Please enter a snippet of text - with this as a start, the app will provide you 5 guesses in ranked order to complete your phrase'),
               textInput('sentence', "Snippet:", value = "Enter some text and have a great"),
               h4('Guesses'),
               p('Below are our top five guesses for the word that completes this snippet'),
               verbatimTextOutput("final.guesses")
            )
         )
      )
))
Run Code Online (Sandbox Code Playgroud)

服务器R

options(expressions = 10000)
quad.grams <- readRDS("quadgrams.rds")
tri.grams <- readRDS("trigrams.rds")
kneser.ney <- readRDS("kneserney.rds")
library(shiny)
library(tm)
library(plyr)
library(stringi)
library(RWeka)
library(stringr)
function(input, output, clientData, session) {
   observe({
      sentence <- reactive({as.character(input$sentence)})
      sentence <- reactive({str_replace_all(sentence(), "[[:punct:]]", "")})
      sentence <- reactive({tolower(sentence())})
      sentence <- reactive({scan_tokenizer(sentence())})
      n <- reactive({length(sentence())})
      tri.frag <- reactive({paste(sentence()[n() - 2], sentence()[n() - 1], sentence()[n()])})
      bi.frag <- reactive({paste(sentence()[n() - 1], sentence()[n()])})
      quad.guesses <- reactive({quad.grams[grepl(tri.frag(), quad.grams$n1.Gram) == TRUE, 2]})
      tri.guesses <- reactive({tri.grams[grepl(bi.frag(), tri.grams$n1.Gram) == TRUE, 2]})
      guesses <- reactive({c(quad.guesses(), tri.guesses(), kneser.ney$nGram)})
      output$final.guesses <- renderPrint({guesses()[1:5]})
   })
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*aul 5

我认为你的问题就在这里(无法测试确定):

observe({
  sentence <- reactive({as.character(input$sentence)})
  sentence <- reactive({str_replace_all(sentence(), "[[:punct:]]", "")})
  sentence <- reactive({tolower(sentence())})
  sentence <- reactive({scan_tokenizer(sentence())})
Run Code Online (Sandbox Code Playgroud)

这四行都是sentence反应性的,每行都依赖于自身,input$sentence而其他每一行都会导致闪亮的事物如何工作变得混乱。更好的解决方案是:

observe({
  sentence1 <- reactive({as.character(input$sentence)})
  sentence2 <- reactive({str_replace_all(sentence1(), "[[:punct:]]", "")})
  sentence3 <- reactive({tolower(sentence2())})
  sentence4 <- reactive({scan_tokenizer(sentence3())})
Run Code Online (Sandbox Code Playgroud)

您也许可以删除该sentence1行 - 我认为input$sentence已经是字符了。如果您愿意,可以合并其余行,例如:

  sentence <- reactive({scan_tokenizer( tolower(
                str_replace_all(input$sentence, "[[:punct:]]", "") ))
             })
Run Code Online (Sandbox Code Playgroud)