Issues integrating shinychat into a modular R Shiny app - Stack Overflow

I am having problems integrating this chat feature into a modular shiny app. Using the chat_ui example

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)
    })
  })
}

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)
    })
  })
}
Share Improve this question edited Mar 10 at 19:05 Jan 10.2k6 gold badges21 silver badges33 bronze badges asked Mar 10 at 14:18 JoshJosh 2,0123 gold badges18 silver badges22 bronze badges 2
  • Did you try chat_ui("chat", fill = TRUE)? I think the chat objects use black magic to find each other. – Michael Dewar Commented Mar 10 at 15:15
  • @MichaelDewar, I did try the code as-is from the shinychat example, without any NS code and it didnt work either.... – Josh Commented Mar 10 at 15:23
Add a comment  | 

1 Answer 1

Reset to default 3

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842326a4596632.html

相关推荐

  • Issues integrating shinychat into a modular R Shiny app - Stack Overflow

    I am having problems integrating this chat feature into a modular shiny app. Using the chat_ui example

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信