2

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)

bretauv
  • 7,756
  • 2
  • 20
  • 57

1 Answers1

8

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

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20