简体   繁体   中英

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.

  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 eg pandoc :

pandoc -t html

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM