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.