Load image from website

前端 未结 3 1186
谎友^
谎友^ 2020-12-12 03:10

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:

相关标签:
3条回答
  • 2020-12-12 03:12

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

    0 讨论(0)
  • 2020-12-12 03:20

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

    0 讨论(0)
  • 2020-12-12 03:32

    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

    0 讨论(0)
提交回复
热议问题