Wrap text output that is rendered with textOutput()


#1

Hi all, I am somewhat new to Shiny development and have what is possibly a novice question. I am trying to wrap text output rendered with the textOutput() function in a UI file.

Here is how the Shiny app appears in the R Studio viewer (wraps fine): https://imgur.com/a/IcHSj

But here is how it appears in a browser (Chrome, in my case): https://imgur.com/fXlWWOJ

So that users do not have to scroll horizontally, how can I write this so that the text wraps in a browser window?

The files for the package are here: https://github.com/jrosen48/shinykonfound


#2

Why not use htmlOutput instead of textOutput? That should fix it and your text would look nicer as well. If you want to stick with textOutput you can use CSS

See example below:

library(shiny)

ui <- fluidPage(
  tags$head(tags$style(HTML("pre { white-space: pre-wrap; word-break: keep-all; }"))),
  textOutput("text")
)

server <- function(input, output) {
   output$text <- renderPrint({
     cat(paste0(rep("print a few words", 100), collapse = " "))
   })
}

shinyApp(ui = ui, server = server)

Interestingly, this is the opposite of a question I just asked :grinning:


#3

Thanks, this is really helpful.

Wonder if you can brainstorm ideas with me here. The function I’m trying to output (HTML) text from prints the output.

It’s a bit clunky but here’s the basic idea (if this could be improved, I welcome suggestions for sure) for the server file:

df <- eventReactive(input$button, {
    pkonfound(as.numeric(input$unstd_beta), 
              as.numeric(input$std_error), 
              as.numeric(input$n_obs), 
              as.numeric(input$n_covariates))
})

output$text <- renderText({
    df()
})

Then the UI file would be something like what you wrote:

ui <- fluidPage(
  tags$head(tags$style(HTML("pre { white-space: pre-wrap; word-break: keep-all; }"))),
  textOutput("text")
)

Any ideas how I could use the print output… as HTML? capture.output()?


#4

Alright, I think capture.output works well. Thanks for pointing me in the right direction.