Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing out sentences on separate lines using textOutput() in Shiny

Tags:

regex

text

r

shiny

I have a few sentences stored in the rows of a dataframe column which I would like to print out line by line in shiny.

The problem is, printing the sentences out using textOutput() conjoins all the sentences together into a single string.

I've tried using verbatimTextOutput and although this prints out the sentences with line breaks, the output format can be messy for large sentences. Also, I would like to avoid the grey background which accompanies verbatimTextOutput.

Can anyone help me in figuring out how to add line breaks between the sentences prior to printing?

Below is a reproducible example:

shinyUI(

        textOutput("text")

        )

shinyServer(function(input, output){


        list <- c("blah blah blah", "drivel drivel drivel", 
                  "blah blah blah")

        text.data <- as.data.frame(list)

        colnames(text.data) <- " "

        output$text <- renderPrint({
                text.data

        })

})

The result produced by the above is:

1 blah blah blah 2 drivel drivel drivel 3 blah blah blah

However, this is the result I'm after:

1 blah blah blah 
2 drivel drivel drivel 
3 blah blah blah  
like image 270
dts86 Avatar asked Sep 16 '25 00:09

dts86


1 Answers

I think you are much better off if you use uiOutput in the ui part of shiny and renderTable in the server part since you try to output a data.frame and not really a text. It will give you what you need:

ui <- shinyUI(

  uiOutput("text")

)

server <- shinyServer(function(input, output){


  list <- c("blah blah blah", "drivel drivel drivel", 
            "blah blah blah")

  text.data <- as.data.frame(list)

  colnames(text.data) <- " "

  output$text <- renderTable({

    print(text.data)

  })

})

runApp(list(ui=ui, server=server))

Output:

enter image description here

like image 76
LyzandeR Avatar answered Sep 17 '25 15:09

LyzandeR