download.file

download.file(url, destfile)
Download a file from a website. This could be a webpage, an R file, a tar.gz file, etc.
  • url – The URL of the file to download.
  • destfile – Where the file should be saved (path with a file name).

Example. The getURL/getURLContent post is downloaded from RFunction.com. (Recall that these functions are used to retrieve web page content.) Next, I run the code downloaded, which retrieves some auction information from Ebay, and then I print the first title of the first auction in the results retrieved.
> 
> #______Download And Run Code ______#
> URL      <- "http://rfunction.com/code/1202/120222.R"
> destfile <- "downloaded/120222.R"
> download.file(URL, destfile)
trying URL 'http://rfunction.com/code/1202/120222.R'
Content type 'text/plain' length 823 bytes
opened URL
==================================================
downloaded 823 bytes

> source(destfile)
> ls()
 [1] "destfile" "hold"     "html"     "i"       
 [5] "IDs"      "t1"       "t2"       "t3"      
 [9] "t4"       "t5"       "titles"   "URL"     
> 
> titles[1]
[1] "The Big Bang Theory: The Complete Fourth Season
(DVD, 2011, 3-Disc Set)"
> 
Tip. To skip the red printout with the status update, specify quiet=TRUE in the function.

Leave a Reply