我正在构建一个Shiny应用程序,它接受用户的文本输入,将最后两个单词与三元组的数据框进行比较,以预测最可能的下一个单词.在server.R下面,我试图ouptut的triPred函数的输出是一个单词.当我加载这个应用程序后,我在应用程序中键入一些文本后出现以下错误 - '参数1(类型'封闭')无法由'cat'处理 - 这似乎与server.R中的最后一行有关.这只是一个单词,我不清楚什么是'cat'失败,即连接.
server.R
library(stringr)
shinyServer(function(input, output) {
triSplit <- function(input) {
el <- unlist(str_split(input," "))
bigram <- paste(el[length(el)-1],el[length(el)])
return(bigram)
}
triPred <- function(input) {
## pulls out end words that match the input bigram
temp_wf_T <- wf_T[wf_T$start == triSplit(input),]
##Picks one of the best options at random based on count
ans <- sample(temp_wf_T$end[temp_wf_T$count == max(temp_wf_T$count)],1)
return(ans) }
##Read in a dataframe of bigrams, their possible completions, and counts of occurence
wf_T<-readRDS("C:/Users/LTM/DataScienceCertificateCapstone/ShinyTest/data/tdm.rds")
##Runs the triPred function to guess the next most likely word
ans <- reactive(triPred(input$sent))
##generates an output variable to display
output$out <- renderText({ans})
})
Run Code Online (Sandbox Code Playgroud)
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel(h1("My Shiny App", align = "center")),
sidebarLayout(
sidebarPanel(helpText("Please enter a sentence you would like me to complete"),
textInput("sent", label = "sentence")),
##########
mainPanel(h1("Best Guess"),
br(),
textOutput("out")
)
)
))
Run Code Online (Sandbox Code Playgroud)
Vin*_*mme 11
由于我无法重现您的应用,因此很难分辨,但您应该尝试:
output$out <- renderText({ans()})或者只是output$out <- renderText(ans()).
如果省略(),则访问反应本身,而不是它的值.有点像你输入foo而不是foo()功能.