4

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.

Jake
  • 145
  • 2
  • 8
  • assuming the dataframe is `d` and the column of png images is `d$png`, try this: ```{r echo = F, results = 'asis'} for (i in d$png) { cat ('\n### ',i,"\n");cat('\n![',i,'](',i,')\n') } ``` – pcantalupo Sep 16 '15 at 02:05

1 Answers1

5
  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

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • Is there an easy way to run all those within R? Like save pander's result to a file and run pandoc to generate the PDF/Html. I found that pander will print the result, but not save to a file. – whatsnext Dec 08 '15 at 16:39
  • @user2149631 use the `Pandoc.brew` function to generate the report or try the more popular `knitr` package – daroczig Dec 08 '15 at 19:20
  • @daroczig: would you have any recommendations to address the requirements for my table problem here: https://stackoverflow.com/questions/60058692/table-in-bookdown-huskydown-with-several-features-citation-caption-url-png-f/60058693#60058693 ? Many thanks in advance! – mavericks Feb 04 '20 at 18:41
  • @daroczig can you help me on this Q [here](https://stackoverflow.com/questions/64756969/grouping-multiple-png-with-sub-headings-in-rmarkdown) – user5249203 Nov 17 '20 at 18:45