Search code examples
rjupyter-notebookjupyter-irkernel

How to display a table of images in R output in a Jupyter notebook?


How do I take a list of image URLs, and display them in an HTML table in a Jupyter notebook with an R kernel?

Here's a list of URLs:

image_urls = c('https://i.sstatic.net/wdQNM.jpg',
  'https://i.sstatic.net/8oysP.jpg')

Here's some code to display one image from an image_url:

library(jpeg)
library(RCurl)

img <- RCurl::getBinaryURL(image_url)
jj <- jpeg::readJPEG(img,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)

Edit: Another way to think of this is, is there functionality like ipython's display? It looks like there might be in https://github.com/IRkernel/repr. I have to read more.


Solution

  • I’m the maintainer of all IRkernel-related projects.

    IRdisplay is the package you’re searching for, specifically display_jpeg:

    library(IRdisplay)
    display_jpeg(file = 'filename.jpg')
    

    Sadly the file parameter doesn’t work with URLs (yet), so you have to manually pass data to it:

    jpeg_data <- RCurl::getBinaryURL(image_url)
    display_jpeg(jpeg_data)