Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny : display text as code on several lines

Tags:

r

shiny

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)

like image 478
bretauv Avatar asked Oct 15 '25 05:10

bretauv


1 Answers

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

like image 127
the-mad-statter Avatar answered Oct 17 '25 19:10

the-mad-statter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!