I am trying to add some chemical structure images to some plots I have created. I am using the ACToR database to access the chemical structures. For example:
Here is an approach that worked for me for a single image (it could be wrapped in a function to be used in the loop):
con <- url("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1",
open='rb')
rawpng <- readBin(con, what='raw', n=50000)
close(con)
png1 <- readPNG(rawpng)
I tested it using:
plot(1:10, type='n')
rasterImage( as.raster(png1), 3,3,8,8 )
It took some guesswork to get the 50000 and that may be different for other files (actually I should have used 48849, but then it really would be likely to change between files).
(Just posting my comment as an answer)
You can use the download.file
function to download files from the web.
In addition, Windows users may have to alter some of the arguments. It seems that mode="wb"
is a necessary argument to download and view these png
files correctly.
So, something like:
download.file("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=80-05-7",
destfile="tmp.png", mode="wb")
worked for me.
Please note that the Bioconductor R package EBImage is capable of loading images directly from an URL and visualizing them:
library(EBImage)
img = readImage("path/to/your/image/file or URL")
display(img, method = "raster")
Cheers,
Andrzej