Access and download images from urls

Hello! I am trying to create a loop that allows me to download and save urls. Currently the urls are values of a variable that I have created.

I found a similar question with code that seems to work when I download just one url, but the looped code is producing an error.

the looped code im using can be found here: Access and download images from urls (which are values of a variable) is

for (i in 1:20) { #change 100 with your number of rows to read through
  myurl <- paste(completeURLS[i,5], sep = "") #you need the exact column number so change 1 to that value and change df to your dataframe's name
  z <- tempfile() #same as above
  download.file(myurl,z,mode="wb")
  pic <- readJPEG(z)
  writeJPEG(pic, paste("image", "i", ".jpg", sep = "")) 
#alternatively you can do writeJPEG(pic, here("desired folder", paste("image", "i", ".jpg", sep = ""))
#you can do setwd to the folder you want to write the files but here is much better
  file.remove(z)
}

it seems that the urls are being downloaded as the code generates this message

trying URL 'http://pbs.twimg.com/media/EEhSN3iUwAAiaYF.jpg'
Content type 'image/jpeg' length 20252 bytes (19 KB)
==================================================
downloaded 19 KB

but then the code stops. when I try to run pic <- readJPEG(z) i get this error message:

Error in readJPEG(z) :
unable to open /var/folders/hs/y00vqp751dqbpglcp35b3y980000gp/T//RtmpoIlB2y/file5b74462fa9cf

So does that mean that the code isn't creating the tempfile?

Any advice would be greatly appreciated! Thank you so much.

Hi, I am not sure what exactly happens, but here are some recommendations: first, you test the code without using the loop. Then, you can create the temporal file in the /tmp folder, using:
z = tempfile(tmpdir = "/tmp"). Also, after downloading the file, print the z and locate this file in your system make sure it exists.
The i inside the paste is a variable and should not be quoted: writeJPEG(pic, paste("image", i, ".jpg", sep = ""))

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hi there,

Thank you so much for taking time to look this over - I really appreciate it!

I removed the quotes around the i and it worked! Amazing!

Thank you.

1 Like