Jake
Jake

Reputation: 145

embed png in rmarkdown table

I have a dataframe in R with a number of attributes about a bunch of sequence motifs. One of the columns contains a path to a png image of the motif. I want to use rmarkdown to save the file as an html page displaying the dataframe or table with all of the attributes and have the PNG images show up. I can't figure out how to do this.

Upvotes: 4

Views: 1879

Answers (1)

daroczig
daroczig

Reputation: 28672

  1. It's always good to start with some reproducible example:

    df <- data.frame(name = c('bicycle', 'binoculars', 'globe'))
    df$url <- paste0('http://fa2png.io/static/images/',
                     df$name, '_000000_64.png')
    
  2. Call pander::pandoc.image to render image markup from the above URLs in markdown:

    library(pander)
    df$url <- sapply(df$url, pandoc.image.return)
    
  3. Render the markdown table:

    pander(df)
    

Resulting in the following table:

-----------------------------------------------------------------------
   name                                url                             
---------- ------------------------------------------------------------
 bicycle    ![](http://fa2png.io/static/images/bicycle_000000_64.png)  

binoculars ![](http://fa2png.io/static/images/binoculars_000000_64.png)

  globe      ![](http://fa2png.io/static/images/globe_000000_64.png)   
-----------------------------------------------------------------------

That can be converted to HTML or whatever other format is required by e.g. pandoc:

pandoc -t html

enter image description here

Upvotes: 5

Related Questions