48

In shiny, I have the following:

  output$sequenceText <- renderText({
    showSequence()
  })

showSequence <- reactive({
  selectedSeqs <- as.numeric(input$sequenceSelect)
  resultString <- ""
  currentString <-""

  for(i in selectedSeqs){
    currentString <- paste(i, toString(myProts[i]), sep = ":")
    resultString <- paste(resultString, currentString, sep = "\n")
  }
  return(resultString)

})

However, it doesn't seem that the new line character is respected. How do I fix that?

Thanks!

user1357015
  • 11,168
  • 22
  • 66
  • 111
  • 1
    Possible duplicate of [Outputting multiple lines of text with renderText() in R shiny](https://stackoverflow.com/questions/23233497/outputting-multiple-lines-of-text-with-rendertext-in-r-shiny) – melwin_jose Jul 07 '17 at 22:34
  • `verbatimTextOutput()` and `renderText()` will respect `\n` characters. – Brian D Feb 19 '20 at 20:58

5 Answers5

82

To my knowledge, there are only two options to display multiple lines within shiny. One way with using verbatimTextOutput which will provide a gray box around you text (personal preference). The other is to use renderUI and htmlOutput to use raw html. Here is a basic working example to demonstrate the results.

require(shiny)
runApp(
  list(
    ui = pageWithSidebar(
      headerPanel("multi-line test"),
      sidebarPanel(
        p("Demo Page.")
      ),
      mainPanel(
        verbatimTextOutput("text"),
        htmlOutput("text2")
      )
    ),
    server = function(input, output){

      output$text <- renderText({
        paste("hello", "world", sep="\n")
      })

      output$text2 <- renderUI({
        HTML(paste("hello", "world", sep="<br/>"))
      })

    }
  )
)

This yields the following figure:

enter image description here

Mathias711
  • 6,568
  • 4
  • 41
  • 58
cdeterman
  • 19,630
  • 7
  • 76
  • 100
  • assuming you already have some text somewhere in your app, e.g., a reactive expression, and it already contains the `\n` newline characters in it. Use `htmlOutput("text3")` with `output$text3 <- renderText({ gsub(pattern = "\\n", replacement = "
    ", yourtext) })`
    – Brian D Feb 19 '20 at 21:16
  • I would use the code below of @Serhii because using HTML can disable other "htmltools" features you want to include, reported here ("You will not be able to use tag related functions. We strongly recommend using R as much as possible and avoid mixing HTML with R.")[https://unleash-shiny.rinterface.com/htmltools-overview.html] – Corina Roca Jun 09 '22 at 15:48
17

for anyone reading this, you might also want to use tags$br(), which you simply have to insert as argument after a piece of text. For example,

tags$div(
    "a piece of text", tags$br(),
    "this will start from the new line now", tags$br(),
    "and this as well",
    "but not this" )
Serhii
  • 355
  • 2
  • 11
16

How about

  output$text2 <- renderUI({
    HTML('hello <br> world')
  })
martin
  • 785
  • 5
  • 17
7

This is a different approach which is perhaps a bit simpler.

ui <- fluidPage(

  htmlOutput("text")
)

server <- function(input, output) {

  output$text <- renderText({

    paste0("<p>", letters[1:10], "</p>")
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
Chris Beeley
  • 591
  • 6
  • 22
1

There is also the write(x, file = "") trick:

renderPrint({
 write(showSequence(), file = "")
})

If the output is a verbatimTextOutput then this works.

Angel
  • 11
  • 2