12

I have an image on disk and want to display it in a markdown cell in an R Jupyter Notebook. How can I go about this?

I know with Python this as simple as importing the Image class from display.

cel
  • 30,017
  • 18
  • 97
  • 117
user3617525
  • 123
  • 1
  • 4

3 Answers3

11

IRdisplay has functions to rich display "stuff", which includes images:

library("IRdisplay")
display_png(file="plot.png)  
Jan Katins
  • 2,219
  • 1
  • 25
  • 35
3

In a markdown cell as you usually would in a Jupyter Python notebook:

<img src="../relative/path/to/img.png">

or

![image](../relative/path/to/img.png)
saladi
  • 3,103
  • 6
  • 36
  • 61
  • 1
    This seems to only work with images from the web, not with locally hosted images – user3617525 Jan 25 '16 at 02:32
  • 2
    @user3617525, this should work if you give a valid **relative path** to the image. I like the syntax `![image](../relative/path/to/img.png)` better, but that's a matter of taste I guess. – cel Jan 25 '16 at 05:19
0

Here is code for the Jupyter R Kernel Notebook User to select choose an image file (.PNG) from the file system, strip off the full pathname, and then insert the image into the cell below this code.

image_chosen = choose.files(
               default = "", 
               caption = "Select The Image File (in PNG format) to Insert",
               multi = TRUE, filters = Filters,
               index = nrow(Filters)
               )

chosen_image_name = basename(image_chosen)
#chosen_image_name   # uncomment this line to show the location of the image.

IRdisplay::display_png(file = chosen_image_name)    # This line inserts the image.

Then use the standard Jupyter Notebook extension "Hide Input" to hide this codecell, and the "Freeze" NBextension to freeze the codecell so the image stays frozen and the notebook does not try to select and insert a new image everytime the notebook code runs.

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44