how to download and display an image from an URL in R?

前端 未结 3 2066
遥遥无期
遥遥无期 2020-12-03 17:24

My goal is to download an image from an URL and then display it in R.

I got an URL and figured out how to download it. But the downloaded file can\'t be previewed be

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 18:05

    If I try your code it looks like the image is downloaded. However, when opened with windows image viewer it also says it is corrupt. The reason for this is that you don't have specified the mode in the download.file statement.

    Try this:

    download.file(y,'y.jpg', mode = 'wb')
    

    For more info about the mode is see ?download.file

    This way at least the file that you downloaded is working.

    To view the image in R, have a look at

    jj <- readJPEG("y.jpg",native=TRUE)
    plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
    rasterImage(jj,0,0,1,1)
    

    or how to read.jpeg in R 2.15 or Displaying images in R in version 3.1.0

提交回复
热议问题