-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.r
127 lines (105 loc) · 3.39 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
get.script.fn <- function() {
args <- commandArgs()
if (2 < length(args)) {
fn <- basename(strsplit(args[grepl("^--file=",args)],"=")[[1]][2])
return(fn)
} else {
return(NULL)
}
}
check.fn <- function(fn) {
if (!file.exists(fn)) {
cat("File does not exist:",fn,"\nexiting...\n")
quit()
}
}
dir.maker <- function(dirpath) {
if (!dir.exists(dirpath)) {
dir.create(dirpath)
cat("Directory has been created:",dirpath,"\n")
}
return(dirpath)
}
DATA.DIR <- dir.maker(file.path(".","data"))
IMG.DIR <- dir.maker(file.path(".","img"))
RESULTS.DIR <- dir.maker(file.path(".","results"))
ARGS <- commandArgs(trailingOnly=T)
ARGV <- c(get.script.fn(),ARGS)
NARGS <- length(ARGS)
get.first.arg.as.fn <- function(wd=getwd()) {
args = commandArgs(trailingOnly=T)
if (length(args) == 0)
stop("Filename should be an argument.")
cat("All args:\n")
print(args)
if(!file.exists(file.path(wd,args[1])))
stop("file not found: '",args[1],"'")
return(args[1])
}
get.raw.df <- function(loc.site,loc.file,force.remote=F,data.dir=DATA.DIR) {
remote.site <- paste0(loc.site,loc.file)
local.path <- file.path(data.dir,loc.file)
if (file.exists(local.path) && !force.remote)
raw.data <- read.csv(local.path)
else {
raw.data <- read.csv(remote.site)
write.csv(raw.data,file=local.path)
}
return(raw.data)
}
stop.if.not.installed <- function(lib.names) {
for(lib.name in lib.names) {
if (!require(lib.name,character.only=T,quietly=T))
stop("Not installed: ",lib.name)
require(lib.name,character.only=T,quietly=T)
}
}
time.stamp <- function() {
ts <- gsub(" ","_",Sys.time())
ts <- gsub("[-:]","",ts)
return(ts)
}
make.string.v <- function(num,maxStrLen=12) {
v <- c()
for (i in 1:num)
v <- append(v,paste0(sample(c(letters," ","'"),sample(maxStrLen,1),replace=T),collapse=""))
v = gsub("^\\s+|\\s+$", "", v) # removing leading and trailing space(s)
return(v)
}
unique.letters <- function(name) {
name <- gsub("\\s", "", name) # [[:space:]] is also good
name <- tolower(name)
letters <- strsplit(name, split="")[[1]]
sort(unique(letters))
}
us.cities <- function(num) {
file.path = './data/us_cities.txt'
if (!file.exists(file.path))
download.file("https://wiki.skullsecurity.org/images/5/54/US_Cities.txt",file.path)
us.cities <- scan(file=file.path,what=character(),sep="\n",quiet=T)
## str(us.cities) # 20500 items
names <- sample(us.cities,num)
return(names)
}
str.rev <- function(str) {
paste0(rev(strsplit(str,split="")[[1]]),collapse="")
}
date.generator <- function(num) {
return(sort(as.Date(sample(unclass(Sys.Date()),num),format="%s",origin="1970-01-01")))
}
time.generator <- function(num) {
return(sort(as.POSIXct(sample(unclass(Sys.time()),num),format="%s",origin="1970-01-01 00:00:00")))
}
check.package <- function(pkg.name,ndebug=T) {
if (!require(pkg.name,quietly=ndebug,character.only=T)) {
install.packages(pkg.name,repos="https://cloud.r-project.org")
if (!require(pkg.name,quietly=ndebug,character.only=T)) {
stop("package not found: ",pkg.name)
}
}
}
check.packages <- function(pkg.names,ndebug=T) {
for (it in pkg.names) {
check.package(it,ndebug)
}
}