-1

I have a shiny app that allows the user to specify a bunch of options for building a statistical model, and the end result is supposed to be the output of a series of regression coefficients, as shown in this screenshot of a simple print() to the console:

correct output shown in the console

However, when I try to render that exact message to my Shiny UI, newlines are ignored, resulting in a jumbled mess of characters, like this:

unmarkedFrame Object 16 sites Maximum number of observations per site: 23 Mean number of observations per site: 4.38 Number of primary survey periods: 23 Number of secondary survey periods: 1 Sites with at least one detection: 16 Tabulation of y observations: 0 1 22 48 298 Site-level covariates: site Elevation Min. : 1.00 Min. : 148.2 1st Qu.: 4.75 1st Qu.:1085.0 Median : 8.50 Median :1318.2 Mean : 8.50 Mean :1604.8 3rd Qu.:12.25 3rd Qu.:2264.2 Max. :16.00 Max. :2958.9 NA's :4 Yearly-site-level covariates: Year 2016 :80 2013 :64 2014 :48 2015 :48 2017 :48 2012 :32 (Other):48 Year 2016 :80 2013 :64 2014 :48 2015 :48 2017 :48 2012 :32 (Other):48

So the question is:

How can I get the text to output to my shiny webpage while maintaining the readable formatting of the console output?

I found the following related question, but neither answer seemed to solve my problem...

how to insert new line in R shiny string

twieg
  • 53
  • 2
  • 9
  • Have you tried using `verbatimTextOutput` with `renderPrint` in server.R? That should give you a look like in the console – Blaza May 18 '18 at 22:31

1 Answers1

0

I figured it out: I needed to use the collapse argument in paste such that <br> was inserted between every item in the character vector capture.output(print(summary(m0))).

The next problem was that whitespace would be ignored (because HTML), which I fixed by sandwiching the output between <pre> tags in order to preserve whitespace verbatim. The solution in server.R looks like this:

output$modelParameters <- renderUI(
        HTML(
          paste(
            c("<pre>", capture.output(print(summary(m0))), "</pre>"),
            collapse = "<br>"
          )
        )
      )

When called in ui.R by htmlOutput("modelParameters"), it outputs the text, formatted appropriately in a grey box, as intended.

twieg
  • 53
  • 2
  • 9