在 rmarkdown 中嵌入 png table

embed png in rmarkdown table

我在 R 中有一个数据框,其中包含关于一堆序列图案的许多属性。其中一列包含指向主题的 png 图像的路径。我想使用 rmarkdown 将文件另存为显示数据框的 html 页面或具有所有属性的 table 并显示 PNG 图像。我不知道该怎么做。

  1. 总是最好从一些可重现的例子开始:

    df <- data.frame(name = c('bicycle', 'binoculars', 'globe'))
    df$url <- paste0('http://fa2png.io/static/images/',
                     df$name, '_000000_64.png')
    
  2. 调用 pander::pandoc.image 以在 markdown 中呈现来自上述 URL 的图像标记:

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

    pander(df)
    

结果如下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)   
-----------------------------------------------------------------------

可以转换为 HTML 或任何其他格式,例如pandoc:

pandoc -t html