Shiny 应用程序失败,“参数 1(类型 'closure')无法由 'cat' 处理"- 这是什么意思? [英] Shiny app fails with "argument 1 (type 'closure') cannot be handled by 'cat'" - what does this mean?
问题描述
我正在构建一个 Shiny 应用程序,它接受用户的文本输入,将最后两个单词与三元组数据框进行比较,以预测最有可能的下一个单词.在 server.R 中,我试图输出的 triPred 函数的输出是一个单词.当我加载这个应用程序时,我在应用程序中输入一些文本后收到以下错误 - 参数 1(类型 'closure')不能由 'cat' 处理 - 这似乎与 server.R 中的最后一行有关这只是一个词,我不清楚猫"即连接有什么问题.
I am building a Shiny app that takes a user's text input, compares the last two words to a data frame of trigrams to predict the most likely next word. In server.R below the output of the triPred function which I am trying to ouptut is a single word. When I load this app I get the following error after I type some text into the app - 'argument 1 (type 'closure') cannot be handled by 'cat' - which appears to be related to the final line in server.R As this is just a single word, I am unclear what is failing with 'cat' ie concatenate.
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})
})
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")
)
)
))
推荐答案
很难说,因为我无法重现您的应用程序,但您应该尝试:
It's hard to tell since I can't reproduce your app, but you should try with:
output$out <- renderText({ans()})
或者只是 output$out <- renderText(ans())
.
如果你省略了 ()
,你访问的是响应式本身,而不是它的值.有点像为函数键入 foo
而不是 foo()
.
If you omit the ()
, you access the reactive itself, and not the value of it. A bit like when you type foo
instead of foo()
for a function.
这篇关于Shiny 应用程序失败,“参数 1(类型 'closure')无法由 'cat' 处理"- 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!