Displaying user entered line breaks in a modal

Hi,

How can I retain line breaks that a user enters in a text box when I display it in a modal?

I have tried to replace the new line character with a break line character but this is getting rendered in the modal

Any pointers greatly appreciated
Thanks

Iain

library(shiny)
library(stringr)
shiny::shinyApp(
  ui = basicPage(
    textAreaInput("text","Text to appear in Modal"),
    actionButton("show", "Show modal dialog")
  ),
  server = function(input, output) {
    observeEvent(input$show, {
      showModal(modalDialog(
        title = "Somewhat important message",
        p(str_replace(input$text,"\n","<br/>")),
        easyClose = TRUE,
        footer = NULL
      ))
    })
  }
)

Hi @iain. You have to convert the string to html by HTML function. And str_replace only replace the first found pattern. If you want to reserve all line breaks, change to gsub.

library(shiny)
library(stringr)
shiny::shinyApp(
  ui = basicPage(
    textAreaInput("text","Text to appear in Modal"),
    actionButton("show", "Show modal dialog")
  ),
  server = function(input, output) {
    observeEvent(input$show, {
      showModal(modalDialog(
        title = "Somewhat important message",
        p(HTML(gsub("\n","<br/>",input$text))),
        easyClose = TRUE,
        footer = NULL
      ))
    })
  }
)
1 Like

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