Search code examples
htmlshinytags

How to use HTML tags on other tags in Shiny?


I am trying to print a title like :

There is 45% available

With 45% in bold like that, and I produced this code :

h4(paste0("There is ", strong(as.character(prctnage)), strong("%"), " available"))

Here is the output :

There is <strong>45<strong><strong>%<strong> available

Any suggestion?


Solution

  • See ?h4: ... arguments expect:

    Tag attributes (named arguments) and children (unnamed arguments)

    In your example paste0 in h4 converts the tags to a string.

    library(shiny)
    library(htmltools)
    
    percentage <- 10
    
    ui <- fluidPage(
      h4("There is ", strong(paste0(percentage, "%")), " available"),
      # another option:
      h4(HTML(paste0("There is ", strong(as.character(percentage)), strong("%"), " available")))
    )
    
    server <- function(input, output, session) {
      
    }
    

    shinyApp(ui, server)

    result

    Another option would be to mark the characters as HTML again using HTML()