r - Load image from website -


i trying add chemical structure images plots have created. using actor database access chemical structures. example:

enter image description here (http://actor.epa.gov/actor/image?format=png%3aw250%2ch250&casrn=80-05-7)

the nice thing site can change size , chemical within url, can automate grabbing images. hope store object containing cas numbers, iterate through cas numbers make 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 png package, , tried use rgdal package well. unfortunately, far can tell, actor generate images in png or jpeg format - cannot use grimport package reading vector images.

i hoping find solution not have manually download each image - there lot of them. open solution r goes , dowloads images folder, use png package, or rgdal package load image , plot them.

in response @ialm: here tried after 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 go open image 7 kb , follow message in image viewer: "windows photo viewer can't open picture because file appears damaged, corrupted, or large."

i should note (against will) using windows 7. tried using both rstudio, , r. rstudio gave me warning message, , r did not - r created appears same file (7kb) , still not open.

in response @greg snow: add context, ran following fresh r console in rstudio. used 64 bit rv3.0.1 , 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" 

here approach worked me single image (it wrapped in function used in 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 using:

plot(1:10, type='n') rasterimage( as.raster(png1), 3,3,8,8 ) 

it took guesswork 50000 , may different other files (actually should have used 48849, change between files).


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -