我在将此聊天功能集成到模块化闪亮应用程序中时遇到了问题。使用CHAT_UI示例和一些轻微的修改使其模块化产生此错误。我认为这与R无法找到输入$ chat_user_input有关,但我不知道该如何修复,因为它都在chat_ui函数中抽象了。
Error in paste(namespace,collapse=ns.sep): cannot coerce type closure to vector of type character
chat_ui
函数,但这也是
shinychat
函数
chat_ui
的名称。这很可能也是该网页中的示例不适合您的原因(根据您的评论),如果您在不覆盖函数的情况下重新启动会话,它应该有效。贝洛(Below)是一个工作示例,我重命名了没有冲突的事物,并且进行了一些调整,以使服务器内没有名称空间问题。
library(shiny)
library(bslib)
library(shinychat)
my_chat_ui <- function(id){
ns <- NS(id)
page_fillable(
chat_ui(
ns("my_chat"),
fill = TRUE
)
)
}
my_chat_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
observeEvent(input$my_chat_user_input, {
# In a real app, this would call out to a chat model or API,
# perhaps using the 'elmer' package.
response <- paste0(
"You said:\n\n",
"<blockquote>",
htmltools::htmlEscape(input$my_chat_user_input),
"</blockquote>"
)
ns <- NS(id)
chat_append(ns("my_chat"), response)
})
}
)
}
server <- function(input, output, session) {
my_chat_server("chat")
}
shinyApp(ui = my_chat_ui("chat"), server = server)