Search code examples
rshinytablehtml

tableHTML in shiny: show an image in a cell


I want to show an image in a shiny UI cell using tableHTML. However, it is simply displaying the text. Any help?

library(shiny)
a = data.frame(rows = 1:2, icons = '<img src = 
   "http://flaglane.com/download/american-flag/american-flag-graphic.png"  
    alt="Red Flag Warning"  height="30" width="40" >')



  shinyApp(
     ui = fluidPage(
      fluidRow(

  br(),
  column(width = 1),
  tableHTML_output("mytable"))
    ), 
 server = function(input, output) {
  output$mytable <- render_tableHTML( 
    tableHTML(a)
  )}
 )

This is what is shown after I run the code: enter image description here


Solution

  • This is a known issue with tableHTML and it is due to be fixed in the next release. For the time being (your link doesn't seem to point to a picture so I ll use one from w3school) you can use this:

    library(shiny)
    library(tableHTML)
    a = data.frame(rows = 1:2, icons = '<img src = 
                   "https://www.w3schools.com/images/w3schools_green.jpg"  
                   alt="Red Flag Warning"  height="30" width="40" >')
    
    
    
    shinyApp(
     ui = fluidPage(
      fluidRow(
    
       br(),
       column(width = 1),
       tableHTML_output("mytable")) 
    
     ), 
     server = function(input, output) {
      output$mytable <- render_tableHTML( 
       tableHTML(a) %>%
        replace_html(pattern = '&#60;', '<', replace_all = TRUE) %>%
        replace_html(pattern = '&#62;', '>', replace_all = TRUE)
      )}
    )
    

    Output:

    enter image description here