Shiny builder elements introduce empty space

Hi,

In Shiny, when I try to build HTML formatted text with multiple elements empty space is introduced. For instance here:
p("Some ", strong("forma", em("tted")), " text")
the word formatted is broken/separated with a space.
Is this intended? and/or avoidable?

Intuitively and due to HTML logic, there should not be any spaces introduced here. I wonder if I am doing something wrong here.

I have realized HTML can be used instead. However, functions strong(), em() can be useful to programmatically insert elements. Therefore, I think the behaviour of those functions is not optimal.

You are not doing anything wrong here. This was an oversight when htmltools was originally created. We are aware of the issue and are trying to come up with a way to maintain old behavior while behaving as you'd expect it to behave.

This produces the html below, which will be displayed with the extra whitespace.

> p("Some ", strong("forma", em("tted")), " text")
<p>
  Some 
  <strong>
    forma
    <em>tted</em>
  </strong>
   text
</p>

While not the prettiest, you can use the code below:

> p("Some ", strong(HTML(paste0("forma", em("tted")))), " text")
<p>
  Some 
  <strong>forma<em>tted</em></strong>
   text
</p>

HTML is used to preserve the html tags. paste0 is used to collect the character values with no spaces.

1 Like

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.