-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.R
80 lines (75 loc) · 2.66 KB
/
common.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Common.R
#
# Toolbox for common functionality.
# Make retrieval information as well as information tagging
# Generic and common
#
# Tow Functions are introduced
# getFile - retrieves a file over the internet
# createFileMetaData - Creates a metadata shadow file with relevant tagging
#
# The separation of these funcitions allow the separate development of
# This generic functionalities wihtout affecting the excercises.
# Tagging as well as file fetching may be improved in the future
# and the excercise may benefit from the performant future development.
# create a meta data shadow to the original file
# whoe name is full.name
#
# Current metadata includes
# Original URL - url
# Donwload Date & time - date
# md5 signature - md5
#
# Args:
# full.name - The file whose meta data we are to keep
# url - origin URL of the file
createFileMetaData <- function(full.name, url) {
meta.file = paste(full.name, "meta", sep=".")
library(tools) # md5sum
writeLines(c(paste("date", Sys.time(), sep=":"),
paste("url", url, sep=":"),
paste("md5", md5sum(full.name), sep=":")),
meta.file)
}
# Retrieve file from the net.
# And creates for it a shadow metadata file
#
# Args:
# url - The url location of the file
# dir - Output directory
# name - Output file name
# unzip - Unzip the downloaded file (Default FALSE)
# quiet - if set to FALSE will display the download progress (default TRUE)
getFile <- function(url, dir, name, unzip=FALSE, quiet = TRUE) {
full.path <- file.path(dir, name)
if (!file.exists(full.path)) {
dir.create(file.path(".", dir), showWarnings = FALSE)
download.file(url, destfile = full.path, method = "curl", quiet = quiet)
createFileMetaData(full.path, url)
if (unzip) unzip(full.path, overwrite = T, exdir=dir)
}
}
# Unloads all user load libraries
.unload.all <- function() {
pkgs <- names(sessionInfo()$otherPkgs)
if (length(pkgs) > 0) {
lapply(paste0('package:', pkgs),
detach, character.only = TRUE, unload = TRUE, force = TRUE)
}
invisible()
}
# Unload all environment variables
# prefixed with dot to not be removed from environemnt
.clc <- function() {
rm(list = ls(.GlobalEnv), envir = .GlobalEnv)
}
# First installs if required and than loads all depended libraries
# Args:
# pkg - a vector of library names
depends.on <- function(pkg) {
sapply(pkg, function(p) {
if (!is.element(pkg, .packages(all.available = TRUE))) { print ("Install"); install.packages(pkg) }
suppressMessages(library(pkg, character.only = TRUE))
})
invisible()
}