Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying images in R in version 3.1.0

Tags:

r

image

I've ran into old stackoverflow posts like this one that use packages like ReadImages and biOps that are now deprecated in R 3.1.0, and are not available anymore.

Can someone tell me how to display an image in R 3.1.0? (Specifically jpeg and png images)

like image 363
hlin117 Avatar asked May 25 '14 23:05

hlin117


People also ask

How do I download an image in R?

This is another one of those situations where the easiest thing to do is to use the RStudio tools. If you're running R through Rstudio, then the easiest way to save your image is to click on the “Export” button in the Plot panel (i.e., the area in Rstudio where all the plots have been appearing).

How do I add an image to a RMD?

To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.


2 Answers

As pointed out by Simon Urbanek in the comments of the linked question, you can do this:

library("jpeg")
jj <- readJPEG("myfile.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)

or (for PNG)

library("png")
pp <- readPNG("myfile.png")
plot.new() 
rasterImage(pp,0,0,1,1)

version 0.1.7 of png, 0.1.8 of jpeg

like image 141
Ben Bolker Avatar answered Sep 21 '22 17:09

Ben Bolker


The package imager can load png, jpeg and bmp images.

library(imager)
im<-load.image("myimage")
plot(im)

It has also many functions for processing and plotting. With extra libraries installed (ffmpeg and ImageMagick), it can even load videos and other images format.

like image 27
Vincent Avatar answered Sep 19 '22 17:09

Vincent