-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperFunction.R
30 lines (29 loc) · 968 Bytes
/
HelperFunction.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
# function that downloads files from the web and verifies their checksum.
# If file already exists in working directory, will use local copy.
load.web.file <- function(
url, md5sum, outfile, zipfile = F) {
# check if local file exists
if (file.exists(outfile)) {
# verify checksum
realsum <- tools::md5sum(outfile)[[1]]
if (realsum != md5sum) stop(sprintf("Local file %s has wrong checksum: %s", outfile, realsum))
# do not delete wrong file, it was already here before
} else {
if(zipfile){
# download file
temp <- tempfile()
download.file(url,temp)
unzip(zipfile = temp, files = outfile, exdir = ".")
} else {
# download file
download.file(url, outfile)
}
# verify checksum
realsum <- tools::md5sum(outfile)[[1]]
if (realsum != md5sum) {
# delete wrong file
unlink(outfile)
stop(sprintf("Remote file %s has wrong checksum: %s", url, realsum))
}
}
}