[73f552]: / R / utils.R

Download this file

153 lines (137 with data), 3.8 kB

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# from timeOmics
check_matrix <- function(X){
# add rownames and colnames if absent, cast into matrix
if(!(is.matrix(X) || is.data.frame(X))) return(FALSE)
if(is.data.frame(X)){
X <- as.matrix(X)
}
if(is.null(rownames(X))){
rownames(X) <- seq_len(nrow(X))
}
if(is.null(colnames(X))){
colnames(X) <- paste0("V", seq_len(ncol(X)))
}
return(X)
}
validate_matrix_X <- function(X, var.name = "'X' "){
# X should be a numeric matrix
X <- check_matrix(X)
if(!is.numeric(X)){
stop(var.name,"must be a numeric matrix/data.frame")
}
# if(any(!X)) stop("X must be a numeric matrix/data.frame")
return(X)
}
validate_list_matrix_X <- function(X, var.name = "'X' "){
if(!is.list(X)){
stop(var.name, "must be a list of matrix/data.frame")
}
X <- lapply(X, validate_matrix_X)
return(X)
}
# is_almostInteger <- function (X)
# {
# if (!is.numeric(X) & !is.vector(X))
# return(FALSE)
# if (length(X) != 1)
# return(FALSE)
# if (!is.finite(X))
# return(FALSE)
# X.round <- round(X)
# if (X == X.round)
# return(TRUE)
# return(FALSE)
# }
#' @importFrom methods is
check_getCluster <- function(X){
if(!(is(X, "cluster.df") || is.null(X))){
stop("cluster must be NULL or a result from getCluster()")
}
#stopifnot(is(X, "cluster.df") || is.null(X))
return(X)
}
check_graph <- function(X){
stopifnot(is(X, "igraph") ||
is(X, "grn") ||
is.list(X) ||
is(X, "list.igraph"))
if(is(X, "list")){
stopifnot(all(as.logical(lapply(X,
function(x) {is(x, "igraph") ||
is(x, "list.igraph")}))))
class(X) <- c("list.igraph", class(X))
}
return(X)
}
check_db <- function(X, var.name = "'db' "){
# ADD list of db
# x is a dataframe with 2 columns (from, to) or igraph
if(!(is(X, "igraph") || is(X, "data.frame"))){
stop(var.name, "must be an igraph or data.frame object")
}
if(is(X, "data.frame") & !(all(c("from", "to") %in% colnames(X)))){
stop(var.name, "must contains the columns 'from' and 'to'")
}
return(X)
}
#' @importFrom purrr is_empty
#' @importFrom stats na.omit
check_vector_char <- function(X,
X.length = NULL,
default = NULL,
var.name = "'X' "){
if(is.null(X)){
return(default)
}
# remove NA
X <- na.omit(X)
if(is_empty(X)){
return(default)
} else if(!is.character(X)){
stop(var.name, "must be a charactor vector")
} else if(!is.null(X.length)){
if(length(X) != X.length){
stop("invalid length")
} else { # good length
return(X)
}
} else{
return(X)
}
# return(default)
}
return_true_false <- function(x, default){
if(is.logical(x)){
if(is.finite(x)){
return(x)
} else { #NA
return(default)
}
} else {
return(default)
}
}
check_single_numeric_value <- function(x,
min = NULL,
max = NULL,
var.name = "'r' "){
if(!is.numeric(x) & !is.matrix(x) & length(x) == 1){
stop(var.name, "must be a numeric value")
}
if(!is.null(min) & !is.null(max)){
if(x < min | x > max){
# internal, no need to check min and max order
stop(var.name, "must be a numeric value between ", min, " and ", max)
}
}
return(x)
}
check_named_vector <- function(X, var.name = "'X' "){
if(!(is(X, 'list') | is(X, "atomic"))){
stop(var.name, "must be a named verctor or list")
}
if(is.null(names(X))){
stop(var.name, "must be a named verctor or list")
}
return(X)
}