Brandon Bertelsen
Brandon Bertelsen

Reputation: 44708

Preview a saved PNG in an R device window

Is there a way print an existing PNG file to the device window, for example, let's say that you have saved a plot as PNG but you would like to preview it (for sanity) before placing it in a presentation or a document.

Ideally, I'd like to be able to 'push' the image into the RStudio 'plots' window.

Upvotes: 27

Views: 21378

Answers (3)

Davor Josipovic
Davor Josipovic

Reputation: 5514

Use ImageMagick library for all your image processing needs. It is brought to you as magick package to R. See the introductory vignette.

install.packages('magick')

img <- magick::image_read('/path/image.png')
plot(img) # or print(img)

Upvotes: 5

SethB
SethB

Reputation: 2090

Baptiste's answer didn't work for me while using RStudio in Linux. I had to remove the system.file() call.

library(png)
img <- readPNG('/path/image.png')
grid::grid.raster(img)

Upvotes: 18

baptiste
baptiste

Reputation: 77124

You can import it and display it in R,

library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
grid::grid.raster(img)

Upvotes: 26

Related Questions