Render icon and text together from server in Shiny

33 Views Asked by At

I cannot manage to have html text and icon together rendered by the Shiny server in the main panel unless I render the icon in the UI independently of the server as well. Here is a minimal example:

library(shiny)

ui <- fluidPage(
  titlePanel("Icon test app"),
  sidebarLayout(
    
    sidebarPanel(
    ),
    
    mainPanel(
      
          htmlOutput("restart"),

          h3("Refresh icon from ui()",icon("refresh"))
    )
  )
)

server <- function(input, output) {
 observe({ output$restart = renderText(

    paste0("<h3>",
           "Refresh icon from server()",
           icon("refresh"),
           "</h3>"
    )
  )
 })
}

shinyApp(ui = ui, server = server)

enter image description here

Now the same example without the text and icon rendered by the ui, which shows then the text rendered by the server alone, i.e. without icon:

library(shiny)

ui <- fluidPage(
  titlePanel("Icon test app"),
  sidebarLayout(
    
    sidebarPanel(
    ),
    
    mainPanel(
      
          htmlOutput("restart"),

          # h3("Refresh icon from ui()",icon("refresh"))
    )
  )
)

server <- function(input, output) {
 observe({ output$restart = renderText(

    paste0("<h3>",
           "Refresh icon from server()",
           icon("refresh"),
           "</h3>"
    )
  )
 })
}

shinyApp(ui = ui, server = server)

enter image description here

Help appreciated, thanks!

2

There are 2 best solutions below

0
MrFlick On BEST ANSWER

The code for icon() when run inside the ui function adds an html dependency to the rendering layer. If you just use the icon() function in the server, the dependency isn't added to the html. You can fix this by manually injecting the dependency into your UI. Add fontawesome::fa_html_dependency() to your UI layout. For example this should work:

ui <- fluidPage(
  fontawesome::fa_html_dependency(),
  titlePanel("Icon test app"),
  sidebarLayout(    
    sidebarPanel(
    ),    
    mainPanel(      
      htmlOutput("restart")
    )
  )
)
0
Clemsang On

One way is:

icon("refresh", lib = "glyphicon")

Boostrap has glyphicon loaded per default contrary to fontawesome.

Within paste that converts HTML to character, fontawesome is not loaded automatically. That's why it is working in your first example where there is also an HTML icon loading the required library.