whitespace in R Shiny

萝らか妹 提交于 2020-07-03 07:41:19

问题


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?


回答1:


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:




回答2:


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




回答3:


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")


来源:https://stackoverflow.com/questions/46766411/whitespace-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!