Reputation: 1992
I am having problems integrating this chat feature into a modular shiny app. Using the chat_ui example and some slight modifications to make it modular generate this error. I think it has something to do with R not being able to find input$chat_user_input but I don't know how to fix that since it's all abstracted inside the chat_ui function.
Error in paste(namespace,collapse=ns.sep): cannot coerce type closure to vector of type character
Link to shinychat page: shinychat
library(shiny)
library(bslib)
library(shinychat)
chat_ui <- function(id){
ns <- NS(id)
page_fillable(
chat_ui(ns("chat"), fill = TRUE)
)
}
chat_server <- function(input, output, session) {
moduleServer(id, function(input,output,session) {
observeEvent(input$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$chat_user_input),
"</blockquote>"
)
chat_append("chat", response)
})
})
}
Upvotes: 2
Views: 66
Reputation: 9468
The main issue is that you define the ui
function with name chat_ui
, but this is also the name of the shinychat
function chat_ui
. This is most probably also the reason why the example from the webpage is not working for you (as per your comment), and if you restart your session without overwriting the function, it should work.
Below is a working example where I renamed things such that there are no conflicts and where I made some adjustments such that there are no namespace issues within the server.
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)
Upvotes: 3