1

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:

enter image description here (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"
dayne
  • 7,504
  • 6
  • 38
  • 56
  • Have you looked at `?download.file`? You could automate downloading the images to a folder of your choosing, and then use some package to read in the png image files. – ialm Aug 23 '13 at 16:07
  • @ialm I just tried using `download.file`, but I get a warning message and the image is unreadable. – dayne Aug 23 '13 at 16:13
  • Trying the first file manually, `download.file("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=80-05-7", destfile="tmp.png")`, worked for me. What warning message are you getting? – ialm Aug 23 '13 at 16:17
  • Hmm, that is strange. I am running R 3.0.1 on Ubuntu, but I don't know if that would make a difference. I would read `?download.file` and try changing some of the function parameters. If that fails, maybe someone more knowledgeable than I will be able to help. – ialm Aug 23 '13 at 16:40
  • 2
    OK, after reading the help file and using my Windows box, setting `mode="wb"` might fix your problem. Try `download.file("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casr‌​n=80-05-7", destfile="tmp.png", mode="wb")` – ialm Aug 23 '13 at 16:43
  • @ialm That worked. I still get the warning message for some reason, but I can open the file. If you want to post that as an answer I will accept it tomorrow if no one comes up with a way to import it directly, without saving the file first. – dayne Aug 23 '13 at 16:59

3 Answers3

2

(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&casr‌​‌​n=80-05-7", 
              destfile="tmp.png", mode="wb")

worked for me.

ialm
  • 8,510
  • 4
  • 36
  • 48
2

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).

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Thanks! This works, but only in R, and not RStudio. Do you have any idea what it would not work in RStudio? When I try and use RStudio I get the following error: "Error in readPNG(rawpng) : libpng error: bad adaptive filter value". – dayne Aug 23 '13 at 18:48
  • I started a discussion on the RStudio support page. http://support.rstudio.org/help/discussions/problems/8464-readpng-not-working-in-rstudio – dayne Aug 23 '13 at 19:05
  • @dayne, I received a similar error (plain R, not rstudio) when experimenting and not getting the full .png file. Did you get the error for the same exact file? if not then it may be a difference in size and you need to increase the number of bytes read by `readBin`. – Greg Snow Aug 23 '13 at 19:23
  • I used your code exactly. I also tried increasing n to 100,000 and 150,000 - both giving the same error. I also tried setting it to 48849. None of them worked, but it is working in the RGui. – dayne Aug 23 '13 at 19:27
  • @dayne, that seems strange. Since I received the similar error when I did not download the entire file my guess is that Rstudio is somehow interfering with the download. Try `str(rawpng)` in each to see if you have the same number of bytes. – Greg Snow Aug 23 '13 at 19:33
  • @dayne, the same code as above is working for me in Rstudio, so maybe check your version of Rstudio (I am using 64 bit for each). – Greg Snow Aug 23 '13 at 19:35
  • This is very frustrating. I installed the latest version of 64-bit RStudio (0.97.551 - I had 0.977.336 before), and it is still giving the same error. I edited my question to (hopefully) give a little more clarification. – dayne Aug 23 '13 at 19:48
  • I am still having the error, and have not gotten any feedback on the RStudio page. Should I post a follow-up question on SO? – dayne Sep 05 '13 at 19:42
1

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

aoles
  • 1,525
  • 10
  • 17