forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_file.R
32 lines (28 loc) · 993 Bytes
/
get_file.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# This script downloads file into the specified directory and take care
# of the OS by itself
get_file <- function(src_url, dest_path, dest_filename, unzip = FALSE) {
dest_dir <- file.path(dest_path)
if(!file.exists(dest_dir)) {
dir.create(dest_dir)
}
dest_full_path_and_filename <- file.path(dest_dir, dest_filename)
if(!file.exists(dest_full_path_and_filename)) {
cat("Downloading file from ", src_url, "\n")
if(Sys.info()["sysname"] == "Windows") {
download.file(src_url, destfile = dest_full_path_and_filename, quiet = TRUE)
} else {
download.file(src_url, destfile = dest_full_path_and_filename, method = "curl", quiet = TRUE)
}
if(file.exists(dest_full_path_and_filename)) {
print("Download finished.")
if(unzip) {
unzip(dest_full_path_and_filename)
}
return(dest_full_path_and_filename)
} else {
print("Download failed.")
}
} else {
return(dest_full_path_and_filename)
}
}