5

I have a problem with text in Shiny Dashboard. I would like to save original text formatting, but shiny removes the whitespaces I want to keep.

output$frame <- renderUI({
    HTML(paste(
               p(strong("Name and Surname:"),("     John Smith"))
               )
         )
  })


tabItem(tabName = "aaa",
        h2("bbb"),
        fluidRow(
                box(width = 6, solidHeader = TRUE, htmlOutput("frame")) 
                )
      ),

Unfortunately I get "Name and Surname: John Smith". I wish to have "Name and Surname: John Smith".

How to solve this problem?

tomsu
  • 371
  • 2
  • 16
  • Shiny probably doesn’t remove anything. But whitespace in HTML isn’t preserved — that’s just the way HTML works. Use CSS formatting instead of whitespace to align your elements. – Konrad Rudolph Oct 16 '17 at 08:58

3 Answers3

8

You can use HTML('&nbsp;') to add 1 whitespace and HTML('&emsp;') to add 1 tab space. In your code it wold be as follows:

output$frame <- renderUI({
        HTML(paste(
          p(strong("Name and Surname:"), HTML('&nbsp;'),HTML('&nbsp;'),"John Smith")
        )
        )
      })

With this you get two white spaces and output looks like this: enter image description here

SBista
  • 7,479
  • 1
  • 27
  • 58
2

I found that we can also use stri_dup(intToUtf8(160), 6) from package stringi.

amrrs
  • 6,215
  • 2
  • 18
  • 27
tomsu
  • 371
  • 2
  • 16
2

I found this oddly difficult to achieve. Just adding the style element to pre-wrap introduced an extra new line:

p(strong("Name and Surname:"),("     John Smith"),style="white-space: pre-wrap")

No other style elements (margin:0, etc) could fix this...so,to get around it, I just converted your strong() to HTML, and it works great:

p(HTML("<b>Name and Surname:</b>         John Smith"),style="white-space: pre-wrap")
mattador
  • 421
  • 4
  • 12