Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file into my working directory

Tags:

r

I would like to download a file directly into my working directory I can do this to a temp directory: download.file("http://www.abc.com/abc.zip",temp) but what do I have to replace temp with to get it to download to the working directory?

like image 709
adam.888 Avatar asked Feb 29 '12 13:02

adam.888


People also ask

How do you wget a file to a specific directory?

Downloading a file to a specific directory When downloading a file, Wget stores it in the current directory by default. You can change that by using the -P option to specify the name of the directory where you want to save the file.


2 Answers

If your url is in a variable, you can use basename to get the "filename" part out of it:

u <- "http://www.abc.com/abc.zip"
basename(u) # "abc.zip"

# downloads to current directory:
download.file(u, basename(u))

# downloads to subdirectory "foo":
download.file(u, file.path("foo", basename(u)))
like image 161
Tommy Avatar answered Oct 26 '22 22:10

Tommy


The second argument of download.file() is destfile and it must be specified. I don't have a Windows machine to test this on, but both of these work on my linux box and I can't see why at least the second won't work on Windows too:

download.file("http://www.abc.com/abc.zip", "./abc.zip")
download.file("http://www.abc.com/abc.zip", "abc.zip")

The second of those indicates that if you just give a filename, the file will be download to the current working directory and saved under the stated name.

like image 38
Gavin Simpson Avatar answered Oct 27 '22 00:10

Gavin Simpson