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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With