Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file from internet via R

Tags:

r

download

I have an url, and I want to download the file via R, I notice that download.file would be helpful, but my problem seems different:

url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile)

It doesn't work! I notice that if my url is in the form of xxx.pdf, then the code above is no problem, otherwise the file that is downloaded is corrupt.

Does anyone know how to solve this problem?

like image 710
PepsiCo Avatar asked Nov 17 '13 06:11

PepsiCo


People also ask

How do I download a text file in R?

file() function in R. Then use the function download. file(url, filename) to download the file. Here, url is a string containing the URL of the file, and filename is the filename of the destination file.


1 Answers

Setting the mode might be required to treat the file as binary data while saving it. If I leave that argument out, I get a blank file, but this way works for me:

url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?
attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile, mode="wb")
like image 100
Troy Avatar answered Sep 18 '22 05:09

Troy