Using cli package with Shiny

I use the cli package a lot in my internal R packages. When I then call internal functions within a Shiny application, it would be really cool to be able to show the cli alert in say a Shiny notification or alert.

The problem I am finding is that the formatting isn't quite right in the notification/alert in Shiny. Presumably because of the icon that is used with cli alert messages.

Simple example below:

library(shiny)
#Alert function
alert_if_numeric <- function(x) {
  if (is.numeric(x)) {
    cli::cli_alert_warning("Input shouldn't be numeric!")
  } else {
    cli::cli_alert_success("Good job!")
  }
  return(invisible(NULL))
}

#UI
ui <- fluidPage(
  actionButton("btn", "btn")
)

#Server
server <- function(input, output) {
  observeEvent(input$btn, {
    tryCatch(
      withCallingHandlers(
        alert_if_numeric(1),
        message = function(m) showNotification(m$message, type = "message"),
        warning = function(w) showNotification(w$message, type = "warning")
      ),
      error = function(e) showNotification(e$message, type = "error")
    )
    tryCatch(
      withCallingHandlers(
        alert_if_numeric("a"),
        message = function(m) showNotification(m$message, type = "message"),
        warning = function(w) showNotification(w$message, type = "warning")
      ),
      error = function(e) showNotification(e$message, type = "error")
    )
  })
}
shinyApp(ui, server)

As you can see, this gives a slightly ugly notification because of the icons. I'd love if there was a solution to get the icons to show within the notifications, or at least hide the ugly text that comes from the icons. Is anybody aware of a solution to this?
image

the ugly text represents the style and colouring for the terminal to use; these can be remove like this

    showNotification(cli::ansi_strip(m$message) , type = "message")

Ah this is perfect! Simple and works great. Thanks a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.