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:
(http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7)
The nice thing about this site is you can change the size and the chemical within the url, so I can automate grabbing the images. My hope was to store an object containing CAS numbers, then iterate through the CAS numbers to make the plots.
For example:
library(png)
casnums <- ("80-05-7","77-40-7","1478-61-1")
image.list <- list()
for(cas in casnums){
image.list[[cas]] <- readPNG(paste0("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=",cas))
}
I have tried using readPNG
from the png
package, and tried to use the rgdal
package as well. Unfortunately, as far as I can tell, ACToR will only generate the images in a png or jpeg format - so I cannot use the grImport
package for reading vector images.
I am really hoping to find a solution where I do not have to manually download each image - there are a lot of them. I would be open to a solution where R goes and dowloads the images to a folder, then I could use something like the png
package, or the rgdal
package to load the image and plot them.
In response to @ialm: Here is what I tried after your first comment:
> download.file(url="http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7",destfile="test.png")
trying URL 'http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7'
Content type 'image/png' length 200 bytes
opened URL
downloaded 6691 bytes
Warning message:
In download.file(url = "http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7", :
downloaded length 6691 != reported length 200
When I go to open the image it is only 7 KB and I get the follow message in the image viewer: "Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large."
I should note that I am (against my will) using Windows 7. I also tried using both RStudio, and R. RStudio gave me the warning message, and R did not - but R created what appears to be the same file (7KB) and still does not open.
In response to @Greg Snow: Just to add some context, I ran the following from a fresh R Console in RStudio. I used 64 bit Rv3.0.1 and 64-bit RStudio v0.97.551.
> library(png)
> search()
[1] ".GlobalEnv" "package:png" "tools:rstudio" "package:stats" "package:graphics" "package:grDevices"
[7] "package:utils" "package:datasets" "package:methods" "Autoloads" "package:base"
> con <- url("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1",open='rb')
> rawpng <- readBin(con, what='raw', n=1e6)
> close(con)
> png1 <- readPNG(rawpng)
Error in readPNG(rawpng) : libpng error: bad adaptive filter value
> ls()
[1] "con" "rawpng"
See Question&Answers more detail:
os