How to write text in listwise in R Shiny?

Hi,

I have been trying to wrap a chunk of text to be presented in a desired manner. I have tried to wrap the text in
tags$div(). But it throws me an error

library(shiny)
#> Warning: package 'shiny' was built under R version 4.0.5

ui = fluidPage(
  textOutput("tx1"), tags$br(),
  textOutput("tx2"), tags$br()
  )
  
server <- function(input, output){
  output$tx1 <- renderText(
    tags$div("Note:
    Some text 1
    More text 2
    ")
  )
  
  output$tx2 <- renderText(
    "Note:
    Some text 1
    More text 2
    "
  )
}


shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:7386
#> Warning: Error in cat: argument 1 (type 'list') cannot be handled by 'cat'

I am getting -

Error in cat: argument 1 (type 'list') cannot be handled by 'cat'

and

Note: Some text 1 More text 2

I want to the output to be shown as -

Note:
Some text 1
More text 2

Is there a way I can achieve my desired output?

Thanks.

if you want to output tagLists /tags (HMTL tags) then dont use textOutput/renderText, use uiOutput/renderUI

1 Like

I believe one way you could do this would be with paste. Such as:

renderText(
    paste('Note:', 'Some text 1', 'More text 2', sep = '\n')
)

You probably want to drop any tags$... in your server, unless you are specifically trying to render a UI element using renderUI. That's probably overkill in this case - I'd just drop the tag and put the function call that I wrote above into your renderTexts.

1 Like

I think I have solved it by putting it into the UI.

library(shiny)
#> Warning: package 'shiny' was built under R version 4.0.5

ui = fluidPage(
  textOutput("tx1"), tags$br(),
  tags$ul(
    "Note:",
    tags$li("Some text 1"),
    tags$li("More text 2")
  ),
  textOutput("tx2"), tags$br()
  )
  
server <- function(input, output){
  
  
  output$tx2 <- renderText(
    "Note:
    Some text 1
    More text 2
    "
  )
}


shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:3230

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.