'R shiny : display text as code on several lines

I would like to display some R code on my shiny app. Therefore, I used verbatimTextOutput but I can't find a way to break lines and to display paragraphs of code.

This solution (Outputting multiple lines of text with renderText() in R shiny) only works with the HTML function and there is no way (to my knowledge) to mix verbatimTextOutput and htmlOutput.

I can display code with tags$code but it is not the appearance I would like (I would prefer the grey background).

Here's a reproducible example :

library(shiny)

ui <- fluidPage(
    mainPanel(htmlOutput("base", placeholder = FALSE))
)

server <- function(input, output) {
  
  output$base <- renderUI({
    tags$code(HTML(paste("just", "some", "code", sep = '<br/>')))
  })
  
}

shinyApp(ui = ui, server = server)



Solution 1:[1]

I have previously used cat() for this purpose:

library(shiny)

ui <- fluidPage(
  mainPanel(verbatimTextOutput("vtout"))
)

server <- function(input, output) {
  output$vtout <- renderPrint({
    cat("just", "some", "code", sep = "\n")
  })
}

shinyApp(ui, server)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 the-mad-statter