Search code examples
htmlrtextshinyline-breaks

Why does R Shiny not recognize my line break command?


I have a simple app where I want to have a text pop up, but because the text is long, I want to add line breaks. For some reason, R isn't recognizing my line breaks, even though I've added
, like I read in this example.

Any help would be greatly appreciated!

library(shiny)

long_text <- paste0(htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult."))


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            br(),
            actionButton(inputId = "text_info", 
                         label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
        ),

        mainPanel(
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    observeEvent(input$text_info, {
        showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Here's what it looks like now:

enter image description here


Solution

  • If you paste after changing the text to HTML, it will be character again.

    library(shiny)
    
    long_text <- htmltools::HTML("I have a lot of text. <br><br>And I want it on different lines.<br><br> This should work, but R is being....<br><br>difficult.")
    
    
    
    ui <- fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          br(),
          actionButton(inputId = "text_info", 
                       label = "My R Sob Story", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B")
        ),
        
        mainPanel(
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      observeEvent(input$text_info, {
        showModal(modalDialog(long_text, title = strong("Why R you doing this to me?")))
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    enter image description here