Search code examples
rshinyalertsweetalertshinywidgets

How to write more than 1 line of text delimited by \n using show_alert()?


I have recently found that there is a possibility to display sweet alerts in shiny using the package shinyWidgets.

I am trying to put more than 1 line of text delimited by \n in the function show_alert() but I cannot find any way to get this.

This is what I have:

enter image description here

This is what I want (ignore the different format of the letters or the indentation, I am not interested in having two lines with different format).

enter image description here

Attempts:

  1. text = paste("This data is....", "Please, be careful with...", sep="\n")
  2. text = paste("This data is....", "\n", "Please, be careful with...")
  3. text = paste0("This data is....", "\n", "Please, be careful with...")
  4. text = "This data is....\nPlease, be careful with..."

Code:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  tags$h2("Sweet Alert examples"),
  actionButton(
    inputId = "success",
    label = "Submit type of data",
    icon = icon("check")
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$success, {
    show_alert(
      title = "Are you sure?",
      text = "This data is....\nPlease, be careful with...",
      type = "warning"
    )
  })

}

if (interactive())
  shinyApp(ui, server)

Does anybody know how to help me?

Thanks in advance


Solution

  • We can use show_alert's html parameter:

    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
      tags$h2("Sweet Alert examples"),
      actionButton(
        inputId = "success",
        label = "Submit type of data",
        icon = icon("check")
      )
    )
    
    server <- function(input, output, session) {
      
      observeEvent(input$success, {
        show_alert(
          title = "Are you sure?",
          text = HTML("This data is....<br>Please, be careful with..."),
          type = "warning",
          html = TRUE
        )
      })
      
    }
    
    if (interactive())
      shinyApp(ui, server)
    

    result