jalapic
jalapic

Reputation: 14212

Inserting line breaks into text output in shiny using cat

I am trying to insert some line breaks using cat and \n into some text output using shiny.

Here are the relevant parts of my ui.R and server.R.

let's say I wanted to get the results of a t-test, and have a separate line for 't', 'df' and 'p-value'

I'm using shinydashboard layout.

sample data

Group_A <- 1:13
Group_B <- 8:19

ui

box(title = "T-test results:", status = "primary",  textOutput("text3"), width=3)

server

output$text3 <- renderPrint({

  obj <-  t.test(Group_A, Group_B)

  cat("t = ", round(obj[[3]],3), "\n, df = ", round(obj[[2]],3), "\n, p-value = ", round(obj[[3]],5)) 

})

This output is shown in the image below:

enter image description here

As you can see, the output is across the screen left to right separated by a comma. Because I want to add a number of results, I would like them to go top to bottom on the screen with a line break after each value.

Does anyone know how to do this in shiny ?

Upvotes: 1

Views: 8588

Answers (2)

user3757140
user3757140

Reputation: 1

Use a paragraph tag with nothing inside like tags$p("a"), see here

Upvotes: 0

cdeterman
cdeterman

Reputation: 19970

Here is a solution using renderUI and htmlOutput

library(shiny)
Group_A <- 1:13
Group_B <- 8:19

runApp(
  list(
    ui = fluidPage(
      htmlOutput("text3")
      ),
    server = function(input, output){
      output$text3 <- renderUI({

        obj <-  t.test(Group_A, Group_B)
        HTML(
          paste0("t = ", round(obj[[3]],3), '<br/>', 
              "df = ", round(obj[[2]],3), '<br/>',
              "p-value = ", round(obj[[3]],5))
        )
      })
    }))

Upvotes: 4

Related Questions