我正在开发一款Shiny应用程序,用于猜测用户输入的文本片段中的下一个单词。为此,我将加载三个数据帧,其中包含对下一个单词的可能猜测,并使用grepl查找用户输入文本末尾的三元或二元匹配。我在“常规”R中构建了这个没有错误或问题,但是当它转换为Shiny时,我收到了这个错误:
错误:评估嵌套太深:无限递归/选项(表达式=)?
正如其他Stack Overflow帖子所建议的那样,我摆弄了选项(expressions =)值,但仍然遇到错误。没有太多其他信息可以指导我,并注意到它没有问题,任何人都可以看到我出错的地方?提前谢谢!
对于后台,我正在运行:Win 7,64位OS | 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")
)
)
)
))
server.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]})
})
}
答案 0 :(得分:3)
我认为你的问题就在这里(无法进行测试):
observe({
sentence <- reactive({as.character(input$sentence)})
sentence <- reactive({str_replace_all(sentence(), "[[:punct:]]", "")})
sentence <- reactive({tolower(sentence())})
sentence <- reactive({scan_tokenizer(sentence())})
这四行都是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())})
您可以删除sentence1
行 - 我认为input$sentence
已经是字符。如果你喜欢,可以将其余的行组合起来:
sentence <- reactive({scan_tokenizer( tolower(
str_replace_all(input$sentence, "[[:punct:]]", "") ))
})
答案 1 :(得分:1)
冒着引起另一个无限循环的风险,我很高兴地说我可以发布自己的答案。问题似乎是重复使用“句子”变量。我想当使用被动时,你不能反复重复使用变量,即使你可能只是在改变它们。无论如何,这段代码现在有效:
server.R
options(shiny.maxRequestSize = 50*1024^2)
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) {
snippet <- reactive({as.character(input$sentence)})
clean.snippet <- reactive({str_replace_all(snippet(), "[[:punct:]]", "")})
lower.snippet <- reactive({tolower(clean.snippet())})
tokens <- reactive({scan_tokenizer(lower.snippet())})
n <- reactive({length(tokens())})
tri.frag <- reactive({paste(tokens()[n() - 2], tokens()[n() - 1], tokens()[n()])})
bi.frag <- reactive({paste(tokens()[n() - 1], tokens()[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]})
}