-
Notifications
You must be signed in to change notification settings - Fork 4
/
.Rprofile
104 lines (88 loc) · 2.85 KB
/
.Rprofile
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
## project
PROJDIR <- tryCatch(rprojroot::find_root(rprojroot::has_file(".Rprofile")),
error=function(e) return(NULL))
if(is.null(PROJDIR)) {
warning("You must run the `set_env.sh` and `set_pkg.R` scripts to set the environment and to install the required packages. /!\\ Otherwise, the scripts provided will not work.")
}
## additional variables
VAR <- c("DATADIR", "RESDIR", "NCORE")
tmp <- sapply(VAR, function(v) assign(x=v, value=Sys.getenv(v), envir=.GlobalEnv))
rm(tmp)
RENV <- file.path(PROJDIR, "src", "R")
PYENV <- file.path(PROJDIR, ".pyenv")
RESDIR <- file.path(PROJDIR, "result")
NCORE <- as.integer(NCORE)
SEED <-NULL
## preload library
library(parallel)
## interactive mode ?
II <- interactive()
################################################################################
## defines custom functions
# function for paraload use: write in chunkout when using paraload
chunkout_flush = function(FILE, ARGS, message) {
if(!is.null(FILE)) {
output = paste0(ARGS, collapse=" / ")
write.table(data.frame(paste0("NOK: ", output)), file=FILE,
append=FALSE, row.names=FALSE,
col.names=FALSE, quote=FALSE)
}
stop(message)
}
# custom error catching for tryCatch
custom_error1 <- function(e) {print(e); return(e);}
custom_error2 <- function(e) {print(e); return(NULL);}
custom_error3 <- function(e) {return(NA);}
check_parallel_error = function(seq) {
tmp <- sapply(1:length(seq),
function(ind) return(any(is.character(seq[[ind]]))))
check_error <- sapply(1:length(seq), function(ind) {
if(tmp[ind]) {
return(any(grepl(pattern="[Ee]rror", x=seq[[ind]])))
} else {
return(FALSE)
}
})
if(any(check_error)) {
msg=head(seq[check_error][[1]],1)
} else {
msg=NULL
}
return(list(error=any(check_error), nbError=sum(check_error), msg=msg))
}
# source an entire directory
source_dir <- function(path, trace = TRUE, ...) {
for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
if(trace) cat(nm,":")
source(file.path(path, nm), ...)
if(trace) cat("\n")
}
}
# create directory
ensure_dir <- function(folder_name) {
if(!dir.exists(folder_name)) dir.create(folder_name, recursive = TRUE)
}
# custom column wise mean and variance
myMean <- function(x, ...) {
tmp <- colwise(mean)(x, ...)
colnames(tmp) <- paste0(colnames(x), ".m")
return(tmp)
}
myVar <- function(x, ...) {
tmp <- colwise(var)(x, ...)
colnames(tmp) <- paste0(colnames(x), ".sd2")
return(tmp)
}
## load data
load_data <- function(filename) {
if(grepl("RData", filename)) {
tmp <- load(filename)
return(sce)
}
else if(grepl("rds", filename)) {
sce <- readRDS(filename)
return(sce)
} else {
return(NULL)
}
}