cute_little_R_functions.R 866 KB
Newer Older
Gael  MILLOT's avatar
Gael MILLOT committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# $same.row.name: logical. Are row names identical ? NULL if no row names in the two 2D datasets
# $row.name: name of rows of the 2 datasets if identical (NULL otherwise)
# $any.id.row.name: logical. Is there any row names identical ? NULL if no row names in the two 2D datasets
# $same.row.name.pos1: position, in data1, of the row names identical in data2
# $same.row.name.pos2: position, in data2, of the row names identical in data1
# $common.row.names: common row names between data1 and data2 (can be a subset of $name or not). NULL if no common row names
# $same.col.name: logical. Are column names identical ? NULL if no col names in the two 2D datasets
# $col.name: name of columns of the 2 datasets if identical (NULL otherwise)
# $any.id.col.name: logical. Is there any column names identical ? NULL if no col names in the two 2D datasets
# $same.col.name.pos1: position, in data1, of the column names identical in data2
# $same.col.name.pos2: position, in data2, of the column names identical in data1
# $common.col.names: common column names between data1 and data2 (can be a subset of $name or not). NULL if no common column names
1013
1014
1015
1016
1017
1018
# $any.id.row: logical. is there identical rows (not considering row names)? NULL if nrow(data1) * nrow(data2) > 1e10
# $same.row.pos1: position, in data1, of the rows identical in data2 (not considering row names). Return "TOO BIG FOR EVALUATION" if nrow(data1) * nrow(data2) > 1e10
# $same.row.pos2: position, in data2, of the rows identical in data1 (not considering row names). Return "TOO BIG FOR EVALUATION" if nrow(data1) * nrow(data2) > 1e10
# $any.id.col: logical. is there identical columns (not considering column names)? NULL if ncol(data1) * ncol(data2) > 1e10
# $same.col.pos1: position in data1 of the cols identical in data2 (not considering column names). Return "TOO BIG FOR EVALUATION" if ncol(data1) * ncol(data2) > 1e10
# $same.col.pos2: position in data2 of the cols identical in data1 (not considering column names). Return "TOO BIG FOR EVALUATION" if ncol(data1) * ncol(data2) > 1e10
Gael  MILLOT's avatar
Gael MILLOT committed
1019
1020
# $identical.object: logical. Are objects identical (including row & column names)?
# $identical.content: logical. Are content objects identical (identical excluding row & column names)?
Gael's avatar
tempo    
Gael committed
1021
1022
1023
1024
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
Gael  MILLOT's avatar
Gael MILLOT committed
1025
# EXAMPLES
1026
# obs1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; obs2 = as.data.frame(matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])), stringsAsFactors = TRUE) ; obs1 ; obs2 ; fun_comp_2d(obs1, obs2)
Gael  MILLOT's avatar
Gael MILLOT committed
1027
# obs1 = matrix(101:110, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; obs2 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; obs1 ; obs2 ; fun_comp_2d(obs1, obs2)
1028
1029
1030
1031
# large matrices
# obs1 = matrix(1:1e6, ncol = 5, dimnames = list(NULL, LETTERS[1:5])) ; obs2 = matrix(as.integer((1:1e6)+1e6/5), ncol = 5, dimnames = list(NULL, LETTERS[1:5])) ; head(obs1) ; head(obs2) ; fun_comp_2d(obs1, obs2)
# WARNING: when comparing content (rows, columns, or total), double and integer data are considered as different -> double(1) != integer(1)
# obs1 = matrix(1:1e6, ncol = 5, dimnames = list(NULL, LETTERS[1:5])) ; obs2 = matrix((1:1e6)+1e6/5, ncol = 5, dimnames = list(NULL, LETTERS[1:5])) ; head(obs1) ; head(obs2) ; fun_comp_2d(obs1, obs2)
Gael  MILLOT's avatar
Gael MILLOT committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# obs1 = matrix(1:10, byrow = TRUE, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; obs2 = matrix(c(1:5, 101:105, 6:10), byrow = TRUE, ncol = 5, dimnames = list(c("a", "z", "b"), c(LETTERS[1:2], "k", LETTERS[5:4]))) ; obs1 ; obs2 ; fun_comp_2d(obs1, obs2)
# obs1 = t(matrix(1:10, byrow = TRUE, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5]))) ; obs2 = t(matrix(c(1:5, 101:105, 6:10), byrow = TRUE, ncol = 5, dimnames = list(c("a", "z", "b"), c(LETTERS[1:2], "k", LETTERS[5:4])))) ; obs1 ; obs2 ; fun_comp_2d(obs1, obs2)
# DEBUGGING
# data1 = matrix(1:10, ncol = 5) ; data2 = matrix(1:10, ncol = 5) # for function debugging
# data1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; data2 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# data1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; data2 = matrix(1:10, ncol = 5) # for function debugging
# data1 = matrix(1:15, byrow = TRUE, ncol = 5, dimnames = list(letters[1:3], LETTERS[1:5])) ; data2 = matrix(1:10, byrow = TRUE, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# data1 = matrix(1:15, ncol = 5, dimnames = list(letters[1:3], LETTERS[1:5])) ; data2 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# data1 = matrix(1:15, ncol = 5, dimnames = list(paste0("A", letters[1:3]), LETTERS[1:5])) ; data2 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# data1 = matrix(1:15, ncol = 5, dimnames = list(letters[1:3], LETTERS[1:5])) ; data2 = matrix(1:12, ncol = 4, dimnames = list(letters[1:3], LETTERS[1:4])) # for function debugging
# data1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; data2 = matrix(101:110, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
1043
1044
# data1 = data.frame(a = 1:3, b= letters[1:3], row.names = LETTERS[1:3], stringsAsFactors = TRUE) ; data2 = data.frame(A = 1:3, B= letters[1:3], stringsAsFactors = TRUE) # for function debugging
# data1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; data2 = as.data.frame(matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])), stringsAsFactors = TRUE) # for function debugging
Gael  MILLOT's avatar
Gael MILLOT committed
1045
# data1 = matrix(1:10, byrow = TRUE, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) ; data2 = matrix(c(1:5, 101:105, 6:10), byrow = TRUE, ncol = 5, dimnames = list(c("a", "z", "b"), c(LETTERS[1:2], "k", LETTERS[5:4]))) # for function debugging
1046
# data1 = table(Exp1 = c("A", "A", "A", "B", "B", "B"), Exp2 = c("A1", "B1", "A1", "C1", "C1", "B1")) ; data2 = data.frame(A = 1:3, B= letters[1:3], stringsAsFactors = TRUE) # for function debugging
1047
# data1 = matrix(1:1e6, ncol = 5, dimnames = list(NULL, LETTERS[1:5])) ; data2 = matrix((1:1e6)+1e6/5, ncol = 5, dimnames = list(NULL, LETTERS[1:5]))
Gael  MILLOT's avatar
Gael MILLOT committed
1048
# function name
1049
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
1050
1051
# end function name
# argument checking
1052
if( ! (any(class(data1) %in% c("data.frame", "table")) | all(class(data1) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data1) %in% c("matrix", "data.frame", "table"))
1053
1054
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A MATRIX, DATA FRAME OR TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1055
}
1056
if( ! (any(class(data2) %in% c("data.frame", "table")) | all(class(data2) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data2) %in% c("matrix", "data.frame", "table"))
1057
1058
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A MATRIX, DATA FRAME OR TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
}
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.2/r_debugging_tools-v1.2.R") ; eval(parse(text = str_basic_arg_check_dev)) # activate this line and use the function to check arguments status
# end argument checking
# main code
same.class <- NULL
class <- NULL
same.dim <- NULL
dim <- NULL
same.row.nb <- NULL
row.nb <- NULL
same.col.nb <- NULL
col.nb <- NULL
same.row.name <- NULL
row.name <- NULL
any.id.row.name <- NULL
same.row.name.pos1 <- NULL
same.row.name.pos2 <- NULL
common.row.names <- NULL
same.col.name <- NULL
any.id.col.name <- NULL
same.col.name.pos1 <- NULL
same.col.name.pos2 <- NULL
common.col.names <- NULL
col.name <- NULL
any.id.row <- NULL
same.row.pos1 <- NULL
same.row.pos2 <- NULL
any.id.col <- NULL
same.col.pos1 <- NULL
same.col.pos2 <- NULL
identical.object <- NULL
identical.content <- NULL
1091
if(identical(data1, data2) & (any(class(data1) %in% c("data.frame", "table")) | all(class(data1) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data1) %in% c("matrix", "data.frame", "table"))
Gael  MILLOT's avatar
Gael MILLOT committed
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
same.class <- TRUE
class <- class(data1)
same.dim <- TRUE
dim <- dim(data1)
same.row.nb <- TRUE
row.nb <- nrow(data1)
same.col.nb <- TRUE
col.nb <- ncol(data1)
same.row.name <- TRUE
row.name <- dimnames(data1)[[1]]
any.id.row.name <- TRUE
same.row.name.pos1 <- 1:row.nb
same.row.name.pos2 <- 1:row.nb
common.row.names <- dimnames(data1)[[1]]
same.col.name <- TRUE
col.name <- dimnames(data1)[[2]]
any.id.col.name <- TRUE
same.col.name.pos1 <- 1:col.nb
same.col.name.pos2 <- 1:col.nb
common.col.names <- dimnames(data1)[[2]]
any.id.row <- TRUE
same.row.pos1 <- 1:row.nb
same.row.pos2 <- 1:row.nb
any.id.col <- TRUE
same.col.pos1 <- 1:col.nb
same.col.pos2 <- 1:col.nb
identical.object <- TRUE
identical.content <- TRUE
Gael  MILLOT's avatar
Gael MILLOT committed
1120
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1121
1122
identical.object <- FALSE
if(all(class(data1) == "table") & length(dim(data1)) == 1){
1123
1124
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 ARGUMENT IS A 1D TABLE. USE THE fun_comp_1d FUNCTION")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1125
1126
}
if(all(class(data2) == "table") & length(dim(data2)) == 1){
1127
1128
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data2 ARGUMENT IS A 1D TABLE. USE THE fun_comp_1d FUNCTION")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1129
1130
1131
}
if( ! identical(class(data1), class(data2))){
same.class <- FALSE
1132
}else if( ! (any(class(data1) %in% c("data.frame", "table")) | all(class(data1) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data1) %in% c("matrix", "data.frame", "table"))
1133
1134
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 AND data2 ARGUMENTS MUST BE EITHER MATRIX, DATA FRAME OR TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1135
1136
1137
}else{
same.class <- TRUE
class <- class(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
1138
}
Gael  MILLOT's avatar
Gael MILLOT committed
1139
1140
if( ! identical(dim(data1), dim(data2))){
same.dim <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
1141
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
same.dim <- TRUE
dim <- dim(data1)
}
if( ! identical(nrow(data1), nrow(data2))){
same.row.nb <- FALSE
}else{
same.row.nb <- TRUE
row.nb <- nrow(data1)
}
if( ! identical(ncol(data1), ncol(data2))){
same.col.nb <- FALSE
}else{
same.col.nb <- TRUE
col.nb <- ncol(data1)
}
# row and col names
if(is.null(dimnames(data1)) & is.null(dimnames(data2))){
same.row.name <- NULL
same.col.name <- NULL
# row and col names remain NULL
}else if((is.null(dimnames(data1)) & ! is.null(dimnames(data2))) | ( ! is.null(dimnames(data1)) & is.null(dimnames(data2)))){
same.row.name <- FALSE
same.col.name <- FALSE
# row and col names remain NULL
}else{
if( ! identical(dimnames(data1)[[1]], dimnames(data2)[[1]])){
same.row.name <- FALSE
# row names remain NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1170
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
same.row.name <- TRUE
row.name <- dimnames(data1)[[1]]
}
# row names
any.id.row.name <- FALSE
if(any(dimnames(data1)[[1]] %in% dimnames(data2)[[1]])){
any.id.row.name <- TRUE
same.row.name.pos1 <- which(dimnames(data1)[[1]] %in% dimnames(data2)[[1]])
}
if(any(dimnames(data2)[[1]] %in% dimnames(data1)[[1]])){
any.id.row.name <- TRUE
same.row.name.pos2 <- which(dimnames(data2)[[1]] %in% dimnames(data1)[[1]])
}
if(any.id.row.name == TRUE){
common.row.names <- unique(c(dimnames(data1)[[1]][same.row.name.pos1], dimnames(data2)[[1]][same.row.name.pos2]))
}
# col names
any.id.col.name <- FALSE
if(any(dimnames(data1)[[2]] %in% dimnames(data2)[[2]])){
any.id.col.name <- TRUE
same.col.name.pos1 <- which(dimnames(data1)[[2]] %in% dimnames(data2)[[2]])
}
if(any(dimnames(data2)[[2]] %in% dimnames(data1)[[2]])){
any.id.col.name <- TRUE
same.col.name.pos2 <- which(dimnames(data2)[[2]] %in% dimnames(data1)[[2]])
}
if(any.id.col.name == TRUE){
common.col.names <- unique(c(dimnames(data1)[[2]][same.col.name.pos1], dimnames(data2)[[2]][same.col.name.pos2]))
}
if( ! identical(dimnames(data1)[[2]], dimnames(data2)[[2]])){
same.col.name <- FALSE
# col names remain NULL
}else{
same.col.name <- TRUE
col.name <- dimnames(data1)[[2]]
}
}
# identical row and col content
if(all(class(data1) == "table")){
as.data.frame(matrix(data1, ncol = ncol(data1)), stringsAsFactors = FALSE)
1211
}else if(all(class(data1) %in% c("matrix", "array"))){
Gael  MILLOT's avatar
Gael MILLOT committed
1212
1213
data1 <- as.data.frame(data1, stringsAsFactors = FALSE)
}else if(all(class(data1) == "data.frame")){
1214
data1 <- data.frame(lapply(data1, as.character), stringsAsFactors = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1215
1216
1217
}
if(all(class(data2) == "table")){
as.data.frame(matrix(data2, ncol = ncol(data2)), stringsAsFactors = FALSE)
1218
}else if(all(class(data2) %in% c("matrix", "array"))){
Gael  MILLOT's avatar
Gael MILLOT committed
1219
1220
data2 <- as.data.frame(data2, stringsAsFactors = FALSE)
}else if(all(class(data2) == "data.frame")){
1221
data2 <- data.frame(lapply(data2, as.character), stringsAsFactors = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1222
1223
1224
1225
}
row.names(data1) <- paste0("A", 1:nrow(data1))
row.names(data2) <- paste0("A", 1:nrow(data2))
if(same.col.nb == TRUE){ # because if not the same col nb, the row cannot be identical
1226
1227
if(all(sapply(data1, FUN = typeof) == "integer") & all(sapply(data2, FUN = typeof) == "integer") & as.double(nrow(data1)) * nrow(data2) <= 1e10){ # as.double(nrow(data1)) to prevent integer overflow because R is 32 bits for integers
same.row.pos1 <- which(c(as.data.frame(t(data1), stringsAsFactors = FALSE)) %in% c(as.data.frame(t(data2), stringsAsFactors = FALSE))) # this work fast with only integers (because 32 bits)
Gael  MILLOT's avatar
Gael MILLOT committed
1228
same.row.pos2 <- which(c(as.data.frame(t(data2), stringsAsFactors = FALSE)) %in% c(as.data.frame(t(data1), stringsAsFactors = FALSE)))
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
}else if(as.double(nrow(data1)) * nrow(data2) <= 1e6){ # as.double(nrow(data1)) to prevent integer overflow because R is 32 bits for integers
same.row.pos1 <- logical(length = nrow(data1)) # FALSE by default
same.row.pos1[] <- FALSE # security
for(i3 in 1:nrow(data1)){
for(i4 in 1:nrow(data2)){
same.row.pos1[i3] <- identical(data1[i3, ], data2[i4, ])
}
}
same.row.pos1 <- which(same.row.pos1)

same.row.pos2 <- logical(length = nrow(data2)) # FALSE by default
same.row.pos2[] <- FALSE # security
for(i3 in 1:nrow(data2)){
for(i4 in 1:nrow(data1)){
same.row.pos2[i3] <- identical(data2[i3, ], data1[i4, ])
}
}
same.row.pos2 <- which(same.row.pos2)
1247
1248
1249
1250
}else{
same.row.pos1 <- "TOO BIG FOR EVALUATION"
same.row.pos2 <- "TOO BIG FOR EVALUATION"
}
1251

Gael  MILLOT's avatar
Gael MILLOT committed
1252
1253
1254
1255
1256
1257
1258
names(same.row.pos1) <- NULL
names(same.row.pos2) <- NULL
if(all(is.na(same.row.pos1))){
same.row.pos1 <- NULL
}else{
same.row.pos1 <- same.row.pos1[ ! is.na(same.row.pos1)]
any.id.row <- TRUE
Gael  MILLOT's avatar
Gael MILLOT committed
1259
}
Gael  MILLOT's avatar
Gael MILLOT committed
1260
1261
1262
1263
1264
1265
1266
1267
if(all(is.na(same.row.pos2))){
same.row.pos2 <- NULL
}else{
same.row.pos2 <- same.row.pos2[ ! is.na(same.row.pos2)]
any.id.row <- TRUE
}
if(is.null(same.row.pos1) & is.null(same.row.pos2)){
any.id.row <- FALSE
1268
1269
1270
1271
}else if(length(same.row.pos1) == 0 & length(same.row.pos2) == 0){
any.id.row <- FALSE
}else if(all(same.row.pos1 == "TOO BIG FOR EVALUATION") & all(same.row.pos2 == "TOO BIG FOR EVALUATION")){
any.id.row <- NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1272
1273
}
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1274
1275
1276
1277
any.id.row <- FALSE
# same.row.pos1 and 2 remain NULL
}
if(same.row.nb == TRUE){ # because if not the same row nb, the col cannot be identical
1278
if(all(sapply(data1, FUN = typeof) == "integer") & all(sapply(data2, FUN = typeof) == "integer") & as.double(ncol(data1)) * ncol(data2) <= 1e10){ # as.double(ncol(data1)) to prevent integer overflow because R is 32 bits for integers
Gael  MILLOT's avatar
Gael MILLOT committed
1279
1280
same.col.pos1 <- which(c(data1) %in% c(data2))
same.col.pos2 <- which(c(data2) %in% c(data1))
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
}else if(as.double(ncol(data1)) * ncol(data2) <= 1e6){ # as.double(ncol(data1)) to prevent integer overflow because R is 32 bits for integers
same.col.pos1 <- logical(length = ncol(data1)) # FALSE by default
same.col.pos1[] <- FALSE # security
for(i3 in 1:ncol(data1)){
for(i4 in 1:ncol(data2)){
same.col.pos1[i3] <- identical(data1[ , i3], data2[ ,i4])
}
}
same.col.pos1 <- which(same.col.pos1)

same.col.pos2 <- logical(length = ncol(data2)) # FALSE by default
same.col.pos2[] <- FALSE # security
for(i3 in 1:ncol(data2)){
for(i4 in 1:ncol(data1)){
same.col.pos2[i3] <- identical(data2[ , i3], data1[ , i4])
}
}
same.col.pos2 <- which(same.col.pos2)
1299
1300
1301
1302
}else{
same.col.pos1 <- "TOO BIG FOR EVALUATION"
same.col.pos2 <- "TOO BIG FOR EVALUATION"
}
Gael  MILLOT's avatar
Gael MILLOT committed
1303
1304
1305
1306
1307
1308
1309
names(same.col.pos1) <- NULL
names(same.col.pos2) <- NULL
if(all(is.na(same.col.pos1))){
same.col.pos1 <- NULL
}else{
same.col.pos1 <- same.col.pos1[ ! is.na(same.col.pos1)]
any.id.col <- TRUE
Gael  MILLOT's avatar
Gael MILLOT committed
1310
}
Gael  MILLOT's avatar
Gael MILLOT committed
1311
1312
if(all(is.na(same.col.pos2))){
same.col.pos2 <- NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1313
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1314
1315
1316
1317
1318
same.col.pos2 <- same.col.pos2[ ! is.na(same.col.pos2)]
any.id.col <- TRUE
}
if(is.null(same.col.pos1) & is.null(same.col.pos2)){
any.id.col <- FALSE
1319
1320
1321
1322
}else if(length(same.col.pos1) == 0 & length(same.col.pos2) == 0){
any.id.col <- FALSE
}else if(all(same.col.pos1 == "TOO BIG FOR EVALUATION") & all(same.col.pos2 == "TOO BIG FOR EVALUATION")){
any.id.col <- NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1323
}
Gael  MILLOT's avatar
Gael MILLOT committed
1324
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1325
1326
any.id.col <- FALSE
# same.col.pos1 and 2 remain NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1327
}
1328
1329
1330
1331
1332
1333
if(same.dim == TRUE){
names(data1) <- NULL
row.names(data1) <- NULL
names(data2) <- NULL
row.names(data2) <- NULL
if(identical(data1, data2)){
Gael  MILLOT's avatar
Gael MILLOT committed
1334
1335
1336
identical.content <- TRUE
}else{
identical.content <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
1337
1338
}
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
identical.content <- FALSE
}
}
output <- list(same.class = same.class, class = class, same.dim = same.dim, dim = dim, same.row.nb = same.row.nb, row.nb = row.nb, same.col.nb = same.col.nb , col.nb = col.nb, same.row.name = same.row.name, row.name = row.name, any.id.row.name = any.id.row.name, same.row.name.pos1 = same.row.name.pos1, same.row.name.pos2 = same.row.name.pos2, common.row.names = common.row.names, same.col.name = same.col.name, col.name = col.name,any.id.col.name = any.id.col.name, same.col.name.pos1 = same.col.name.pos1, same.col.name.pos2 = same.col.name.pos2, common.col.names = common.col.names, any.id.row = any.id.row, same.row.pos1 = same.row.pos1, same.row.pos2 = same.row.pos2, any.id.col = any.id.col, same.col.pos1 = same.col.pos1, same.col.pos2 = same.col.pos2, identical.object = identical.object, identical.content = identical.content)
return(output)
}


######## fun_comp_list() #### comparison of two lists


# Check OK: clear to go Apollo
fun_comp_list <- function(data1, data2){
# AIM
# compare two lists. Check and report in a list if the 2 datasets have:
# same length
# common names
# common compartments
# ARGUMENTS
# data1: list
# data2: list
# RETURN
# a list containing:
# $same.length: logical. Are number of elements identical?
# $length: number of elements in the 2 datasets (NULL otherwise)
# $same.name: logical. Are element names identical ?
# $name: name of elements of the 2 datasets if identical (NULL otherwise)
# $any.id.name: logical. Is there any element names identical ?
# $same.name.pos1: position, in data1, of the element names identical in data2
# $same.name.pos2: position, in data2, of the compartment names identical in data1
# $any.id.compartment: logical. is there any identical compartments ?
# $same.compartment.pos1: position, in data1, of the compartments identical in data2
# $same.compartment.pos2: position, in data2, of the compartments identical in data1
# $identical.object: logical. Are objects identical (kind of object, compartment names and content)?
# $identical.content: logical. Are content objects identical (identical compartments excluding compartment names)?
Gael's avatar
tempo    
Gael committed
1374
1375
1376
1377
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
Gael  MILLOT's avatar
Gael MILLOT committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
# EXAMPLES
# obs1 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) ; obs2 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) ; fun_comp_list(obs1, obs2)
# obs1 = list(1:5, LETTERS[1:2]) ; obs2 = list(a = 1:5, b = LETTERS[1:2]) ; fun_comp_list(obs1, obs2)
# obs1 = list(b = 1:5, c = LETTERS[1:2]) ; obs2 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) ; fun_comp_list(obs1, obs2)
# obs1 = list(b = 1:5, c = LETTERS[1:2]) ; obs2 = list(LETTERS[5:9], matrix(1:6), 1:5) ; fun_comp_list(obs1, obs2)
# DEBUGGING
# data1 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) ; data2 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) # for function debugging
# data1 = list(a = 1:5, b = LETTERS[1:2]) ; data2 = list(a = 1:5, b = LETTERS[1:2], d = matrix(1:6)) # for function debugging
# function name
1387
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
1388
1389
1390
# end function name
# argument checking
if( ! any(class(data1) %in% "list")){
1391
1392
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A LIST")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1393
1394
}
if( ! any(class(data2) %in% "list")){
1395
1396
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A LIST")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
}
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.2/r_debugging_tools-v1.2.R") ; eval(parse(text = str_basic_arg_check_dev)) # activate this line and use the function to check arguments status
# end argument checking
# main code
same.length <- NULL
length <- NULL
same.name <- NULL
name <- NULL
any.id.name <- NULL
same.name.pos1 <- NULL
same.name.pos2 <- NULL
any.id.compartment <- NULL
same.compartment.pos1 <- NULL
same.compartment.pos2 <- NULL
identical.object <- NULL
identical.content <- NULL
if(identical(data1, data2)){
same.length <- TRUE
length <- length(data1)
if( ! is.null(names(data1))){
same.name <- TRUE
name <- names(data1)
any.id.name <- TRUE
same.name.pos1 <- 1:length(data1)
same.name.pos2 <- 1:length(data2)
}
any.id.compartment <- TRUE
same.compartment.pos1 <- 1:length(data1)
same.compartment.pos2 <- 1:length(data2)
identical.object <- TRUE
identical.content <- TRUE
}else{
identical.object <- FALSE
if( ! identical(length(data1), length(data2))){
same.length<- FALSE
}else{
same.length<- TRUE
length <- length(data1)
}
if( ! (is.null(names(data1)) & is.null(names(data2)))){
if( ! identical(names(data1), names(data2))){
same.name <- FALSE
}else{
same.name <- TRUE
name <- names(data1)
}
any.id.name <- FALSE
if(any(names(data1) %in% names(data2))){
any.id.name <- TRUE
same.name.pos1 <- which(names(data1) %in% names(data2))
}
if(any(names(data2) %in% names(data1))){
any.id.name <- TRUE
same.name.pos2 <- which(names(data2) %in% names(data1))
}
}
names(data1) <- NULL
names(data2) <- NULL
any.id.compartment <- FALSE
if(any(data1 %in% data2)){
any.id.compartment <- TRUE
same.compartment.pos1 <- which(data1 %in% data2)
}
if(any(data2 %in% data1)){
any.id.compartment <- TRUE
same.compartment.pos2 <- which(data2 %in% data1)
}
if(same.length == TRUE & ! all(is.null(same.compartment.pos1), is.null(same.compartment.pos2))){
if(identical(same.compartment.pos1, same.compartment.pos2)){
identical.content <- TRUE
}else{
identical.content <- FALSE
}
}else{
identical.content <- FALSE
}
}
output <- list(same.length = same.length, length = length, same.name = same.name, name = name, any.id.name = any.id.name, same.name.pos1 = same.name.pos1, same.name.pos2 = same.name.pos2, any.id.compartment = any.id.compartment, same.compartment.pos1 = same.compartment.pos1, same.compartment.pos2 = same.compartment.pos2, identical.object = identical.object, identical.content = identical.content)
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
1476
1477
1478
}


1479
######## fun_test() #### test combinations of argument values of a function and return errors (and graphs)
Gael  MILLOT's avatar
Gael MILLOT committed
1480
1481


Gael  MILLOT's avatar
Gael MILLOT committed
1482
1483
# add traceback https://stackoverflow.com/questions/47414119/how-to-read-a-traceback-in-r

Gael  MILLOT's avatar
Gael MILLOT committed
1484
# Check OK: clear to go Apollo
Gael's avatar
tempo    
Gael committed
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
fun_test <- function(
fun, 
arg, 
val, 
expect.error = NULL, 
thread.nb = NULL, 
print.count = 10, 
plot.fun = FALSE, 
export = FALSE, 
res.path = NULL, 
lib.path = NULL, 
cute.path = "C:\\Users\\Gael\\Documents\\Git_projects\\cute_little_R_functions\\cute_little_R_functions.R"
){
Gael  MILLOT's avatar
Gael MILLOT committed
1498
1499
# AIM
# test combinations of argument values of a function
Gael's avatar
tempo    
Gael committed
1500
# WARNINGS
Gael  MILLOT's avatar
Gael MILLOT committed
1501
# Limited to 43 arguments with at least 2 values each. The total number of arguments tested can be more if the additional arguments have a single value. The limit is due to nested "for" loops (https://stat.ethz.ch/pipermail/r-help/2008-March/157341.html), but it should not be a problem since the number of tests would be 2^43 > 8e12
Gael  MILLOT's avatar
Gael MILLOT committed
1502
# ARGUMENTS
1503
1504
# fun: character string indicating the name of the function tested (without brackets)
# arg: vector of character strings of arguments of fun. At least arguments that do not have default values must be present in this vector
Gael  MILLOT's avatar
Gael MILLOT committed
1505
# val: list with number of compartments equal to length of arg, each compartment containing values of the corresponding argument in arg. Each different value must be in a list or in a vector. For instance, argument 3 in arg is a logical argument (values accepted TRUE, FALSE, NA). Thus, compartment 3 of val can be either list(TRUE, FALSE, NA), or c(TRUE, FALSE, NA)
1506
# expect.error: list of exactly the same structure as val argument, but containing FALSE or TRUE, depending on whether error is expected (TRUE) or not (FALSE) for each corresponding value of val. A message is returned depending on discrepancies between the expected and observed errors. BEWARE: not always possible to write the expected errors for all the combination of argument values. Ignored if NULL
1507
# thread.nb: numeric value indicating the number of available threads. Write NULL if no parallelization wanted
Gael  MILLOT's avatar
Gael MILLOT committed
1508
# print.count: interger value. Print a working progress message every print.count during loops. BEWARE: can increase substentially the time to complete the process using a small value, like 10 for instance. Use Inf is no loop message desired
Gael  MILLOT's avatar
Gael MILLOT committed
1509
# plot.fun: logical. Plot the plotting function tested for each test?
1510
1511
# export: logical. Export the results into a .RData file and into a .txt file? If FALSE, return a list into the console (see below). BEWARE: will be automatically set to TRUE if thread.nb is not NULL. This means that when using parallelization, the results are systematically exported, not returned into the console
# res.path: character string indicating the absolute pathway of folder where the txt results and pdfs, containing all the plots, will be saved. Several txt and pdf, one per thread, if parallelization. Ignored if export is FALSE. Must be specified if thread.nb is not NULL or if export is TRUE
1512
# lib.path: character vector specifying the absolute pathways of the directories containing the required packages if not in the default directories. Ignored if NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1513
# cute.path: character string indicating the absolute path of the cute.R file. Will be remove when cute will be a package. Not considered if thread.nb is NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1514
# REQUIRED PACKAGES
Gael  MILLOT's avatar
Gael MILLOT committed
1515
# lubridate
Gael's avatar
tempo    
Gael committed
1516
# parallel if thread.nb argument is not NULL (included in the R installation packages)
Gael  MILLOT's avatar
Gael MILLOT committed
1517
# if the tested function is in a package, this package must be imported first (no parallelization) or must be in the classical R package folder indicated by the lib.path argument (parallelization)
Gael  MILLOT's avatar
Gael MILLOT committed
1518
# RETURN
Gael  MILLOT's avatar
Gael MILLOT committed
1519
# if export is FALSE a list containing:
Gael  MILLOT's avatar
Gael MILLOT committed
1520
1521
1522
1523
1524
# $fun: the tested function
# $data: a data frame of all the combination tested, containing the following columns:
# the different values tested, named by arguments
# $kind: a vector of character strings indicating the kind of test result: either "ERROR", or "WARNING", or "OK"
# $problem: a logical vector indicating if error or not
1525
# $expected.error: optional logical vector indicating the expected error specified in the expect.error argument
1526
1527
# $message: either NULL if $kind is always "OK", or the messages
# $instruction: the initial instruction
Gael  MILLOT's avatar
Gael MILLOT committed
1528
# $sys.info: system and packages info
1529
# if export is TRUE 1) the same list object into a .RData file, 2) also the $data data frame into a .txt file, and 3) if expect.error is non NULL and if any discrepancy, the $data data frame into a .txt file but containing only the rows with discrepancies between expected and observed errors
Gael  MILLOT's avatar
Gael MILLOT committed
1530
# one or several pdf if a plotting function is tested and if the plot.fun argument is TRUE
Gael's avatar
tempo    
Gael committed
1531
1532
1533
1534
1535
1536
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# fun_get_message()
# fun_pack()
Gael  MILLOT's avatar
Gael MILLOT committed
1537
1538
1539
# EXAMPLES
# fun_test(fun = "unique", arg = c("x", "incomparables"), val = list(x = list(1:10, c(1,1,2,8), NA), incomparable = c(TRUE, FALSE, NA)))
# fun_test(fun = "fun_round", arg = c("data", "dec.nb", "after.lead.zero"), val = list(L1 = list(c(1, 1.0002256, 1.23568), "a", NA), L2 = list(2, c(1,3), NA), L3 = c(TRUE, FALSE, NA)))
1540
# fun_test(fun = "plot", arg = c("x", "y"), val = list(x = list(1:10, 12:13, NA, (1:10)^2), y = list(1:10, NA, NA)),  expect.error = list(x = list(FALSE, TRUE, TRUE, FALSE), y = list(FALSE, TRUE, TRUE)), thread.nb = NULL, plot.fun = TRUE, res.path = "C:\\Users\\Gael\\Desktop\\", lib.path = NULL)
1541
# fun_test(fun = "plot", arg = c("x", "y"), val = list(x = list(1:10, 12:13, NA, (1:10)^2), y = list(1:10, NA, NA)), thread.nb = 4, plot.fun = TRUE, res.path = "C:\\Users\\Gael\\Desktop\\", lib.path = "C:\\Program Files\\R\\R-4.0.2\\library\\")
1542
1543
1544
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; fun_test(fun = "fun_gg_boxplot", arg = c("data1", "y", "categ"), val = list(L1 = list(L1 = obs1), L2 = list(L1 = "Time"), L3 = list(L1 = "Group1")))
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; fun_test(fun = "fun_gg_boxplot", arg = c("data1", "y", "categ"), val = list(L1 = list(obs1), L2 = "Time", L3 = "Group1"), thread.nb = NULL, plot.fun = TRUE, res.path = "C:\\Users\\Gael\\Desktop\\", lib.path = "C:\\Program Files\\R\\R-4.0.2\\library\\")
# library(ggplot2) ; fun_test(fun = "geom_histogram", arg = c("data", "mapping"), val = list(x = list(data.frame(X = "a", stringsAsFactors = TRUE)), y = list(ggplot2::aes(x = X))), thread.nb = NULL, plot.fun = TRUE, res.path = "C:\\Users\\Gael\\Desktop\\", lib.path = "C:\\Program Files\\R\\R-4.0.2\\library\\") # BEWARE: ggplot2::geom_histogram does not work
Gael  MILLOT's avatar
Gael MILLOT committed
1545
# DEBUGGING
1546
1547
1548
# fun = "unique" ; arg = "x" ; val = list(x = list(1:10, c(1,1,2,8), NA)) ; expect.error = list(x = list(FALSE, FALSE, TRUE)) ; thread.nb = NULL ; plot.fun = FALSE ; export = FALSE ; res.path = "C:\\Users\\Gael\\Desktop\\" ; lib.path = NULL ; print.count = 1 ; cute.path = "C:\\Users\\Gael\\Documents\\Git_projects\\cute_little_R_functions\\cute_little_R_functions.R" # for function debugging
# fun = "unique" ; arg = c("x", "incomparables") ; val = list(x = list(1:10, c(1,1,2,8), NA), incomparable = c(TRUE, FALSE, NA)) ; expect.error = NULL ; thread.nb = 2 ; plot.fun = FALSE ; export = TRUE ; res.path = "C:\\Users\\Gael\\Desktop\\" ; lib.path = NULL ; print.count = 10 ; cute.path = "C:\\Users\\Gael\\Documents\\Git_projects\\cute_little_R_functions\\cute_little_R_functions.R" # for function debugging
# fun = "plot" ; arg = c("x", "y") ; val = list(x = list(1:10, 12:13, NA), y = list(1:10, NA, NA)) ; expect.error = list(x = list(FALSE, FALSE, TRUE, FALSE), y = list(FALSE, TRUE, TRUE)) ; print.count = 10 ; thread.nb = NULL ; plot.fun = TRUE ; export = TRUE ; res.path = "C:\\Users\\Gael\\Desktop\\" ; lib.path = NULL # for function debugging
1549
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10), stringsAsFactors = TRUE) ; fun = "fun_gg_boxplot" ; arg = c("data1", "y", "categ") ; val = list(L1 = list(L1 = obs1), L2 = list(L1 = "Time"), L3 = list(L1 = "Group1")) ; expect.error = NULL ; print.count = 10 ; thread.nb = NULL ; plot.fun = TRUE ; export = TRUE ; res.path = "C:\\Users\\Gael\\Desktop\\" ; lib.path = NULL # for function debugging
Gael  MILLOT's avatar
Gael MILLOT committed
1550
# function name
1551
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
1552
instruction <- match.call()
Gael  MILLOT's avatar
Gael MILLOT committed
1553
1554
1555
1556
1557
1558
1559
1560
1561
# end function name
# required function checking
req.function <- c(
"fun_check", 
"fun_get_message", 
"fun_pack"
)
for(i1 in req.function){
if(length(find(i1, mode = "function")) == 0){
1562
1563
tempo.cat <- paste0("ERROR IN ", function.name, ": REQUIRED ", i1, "() FUNCTION IS MISSING IN THE R ENVIRONMENT")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1564
1565
1566
}
}
# end required function checking
Gael  MILLOT's avatar
Gael MILLOT committed
1567
# argument primary checking
1568
1569
# arg with no default values
if(any(missing(fun) | missing(arg) | missing(val))){
1570
1571
tempo.cat <- paste0("ERROR IN ", function.name, ": ARGUMENTS fun, arg AND val HAVE NO DEFAULT VALUE AND REQUIRE ONE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
1572
1573
1574
}
# end arg with no default values
# using fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
1575
1576
1577
1578
1579
1580
1581
1582
1583
arg.check <- NULL #
text.check <- NULL #
checked.arg.names <- NULL # for function debbuging: used by r_debugging_tools
ee <- expression(arg.check <- c(arg.check, tempo$problem) , text.check <- c(text.check, tempo$text) , checked.arg.names <- c(checked.arg.names, tempo$fun.name))
tempo <- fun_check(data = fun, class = "vector", mode = "character", length = 1, fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
if(grepl(x = fun, pattern = "()$")){ # remove ()
fun <- sub(x = fun, pattern = "()$", replacement = "")
}
Gael  MILLOT's avatar
Gael MILLOT committed
1584
1585
if( ! exists(fun)){
tempo.cat <- paste0("ERROR IN ", function.name, ": CHARACTER STRING IN fun ARGUMENT DOES NOT EXIST IN THE R WORKING ENVIRONMENT: ", paste(fun, collapse = "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1586
1587
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
1588
}else if( ! all(class(get(fun)) == "function")){ # here no env = sys.nframe(), inherit = FALSE for get() because fun is a function in the classical scope
1589
tempo.cat <- paste0("ERROR IN ", function.name, ": fun ARGUMENT IS NOT CLASS \"function\" BUT: ", paste(class(get(fun)), collapse = "\n"), "\nCHECK IF ANY CREATED OBJECT WOULD HAVE THE NAME OF THE TESTED FUNCTION")
Gael  MILLOT's avatar
Gael MILLOT committed
1590
1591
1592
1593
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1594
tempo <- fun_check(data = arg, class = "vector", mode = "character", fun.name = function.name) ; eval(ee)
1595
1596
1597
1598
1599
if(tempo$problem == FALSE & length(arg) == 0){
tempo.cat <- paste0("ERROR IN ", function.name, ": arg ARGUMENT CANNOT BE LENGTH 0")
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
Gael  MILLOT's avatar
Gael MILLOT committed
1600
1601
tempo <- fun_check(data = val, class = "list", fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
1602
for(i2 in 1:length(val)){
Gael  MILLOT's avatar
Gael MILLOT committed
1603
1604
tempo1 <- fun_check(data = val[[i2]], class = "vector", na.contain = TRUE, fun.name = function.name)
tempo2 <- fun_check(data = val[[i2]], class = "list", na.contain = TRUE, fun.name = function.name)
Gael  MILLOT's avatar
Gael MILLOT committed
1605
if(tempo1$problem == TRUE & tempo2$problem == TRUE){
1606
tempo.cat <- paste0("ERROR IN ", function.name, ": COMPARTMENT ", i2, " OF val ARGUMENT MUST BE A VECTOR OR A LIST")
Gael  MILLOT's avatar
Gael MILLOT committed
1607
1608
1609
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}else if(tempo1$problem == FALSE){ # vector split into list compartments
1610
val[[i2]] <- split(x = val[[i2]], f = 1:length(val[[i2]]))
Gael  MILLOT's avatar
Gael MILLOT committed
1611
1612
1613
}
}
}
1614
1615
1616
1617
if( ! is.null(expect.error)){
tempo <- fun_check(data = expect.error, class = "list", fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
for(i3 in 1:length(expect.error)){
Gael  MILLOT's avatar
Gael MILLOT committed
1618
1619
tempo1 <- fun_check(data = expect.error[[i3]], class = "vector",  mode = "logical", fun.name = function.name)
tempo2 <- fun_check(data =  expect.error[[i3]], class = "list", fun.name = function.name)
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
if(tempo1$problem == TRUE & tempo2$problem == TRUE){
tempo.cat <- paste0("ERROR IN ", function.name, ": COMPARTMENT ", i3, " OF expect.error ARGUMENT MUST BE TRUE OR FALSE")
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}else if(tempo1$problem == FALSE){ # vector split into list compartments
expect.error[[i3]] <- split(x = expect.error[[i3]], f = 1:length(expect.error[[i3]]))
}
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1630
if( ! is.null(thread.nb)){
1631
tempo <- fun_check(data = thread.nb, typeof = "integer", double.as.integer.allowed = TRUE, neg.values = FALSE, length = 1, fun.name = function.name) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
1632
1633
1634
1635
1636
1637
if(tempo$problem == FALSE & thread.nb < 1){
tempo.cat <- paste0("ERROR IN ", function.name, ": thread.nb PARAMETER MUST EQUAL OR GREATER THAN 1: ", thread.nb)
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1638
tempo <- fun_check(data = print.count, class = "vector", typeof = "integer", length = 1, double.as.integer.allowed = TRUE, neg.values = FALSE, fun.name = function.name) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
1639
tempo <- fun_check(data = plot.fun, class = "vector", mode = "logical", length = 1, fun.name = function.name) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
1640
1641
tempo <- fun_check(data = export, class = "vector", mode = "logical", length = 1, fun.name = function.name) ; eval(ee)
if( ! is.null(res.path)){
Gael  MILLOT's avatar
Gael MILLOT committed
1642
1643
1644
1645
tempo <- fun_check(data = res.path, class = "vector", mode = "character", fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
if( ! all(dir.exists(res.path))){ # separation to avoid the problem of tempo$problem == FALSE and res.path == NA
tempo.cat <- paste0("ERROR IN ", function.name, ": DIRECTORY PATH INDICATED IN THE res.path ARGUMENT DOES NOT EXISTS:\n", paste(res.path, collapse = "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1646
1647
1648
1649
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1650
}
Gael  MILLOT's avatar
Gael MILLOT committed
1651
if( ! is.null(lib.path)){
Gael  MILLOT's avatar
Gael MILLOT committed
1652
1653
1654
1655
tempo <- fun_check(data = lib.path, class = "vector", mode = "character", fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
if( ! all(dir.exists(lib.path))){ # separation to avoid the problem of tempo$problem == FALSE and lib.path == NA
tempo.cat <- paste0("ERROR IN ", function.name, ": DIRECTORY PATH INDICATED IN THE lib.path ARGUMENT DOES NOT EXISTS:\n", paste(lib.path, collapse = "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1656
1657
1658
1659
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1660
}
Gael  MILLOT's avatar
Gael MILLOT committed
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
if( ! is.null(thread.nb)){
tempo <- fun_check(data = cute.path, class = "vector", typeof = "character", length = 1, fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
if( ! file.exists(cute.path)){
tempo.cat <- paste0("ERROR IN ", function.name, ": FILE PATH INDICATED IN THE cute.path PARAMETER DOES NOT EXISTS:\n", cute.path)
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1671
1672
1673
if(any(arg.check) == TRUE){
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) #
}
1674
# end using fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
1675
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.2/r_debugging_tools-v1.2.R") ; eval(parse(text = str_basic_arg_check_dev)) ; eval(parse(text = str_arg_check_with_fun_check_dev)) # activate this line and use the function (with no arguments left as NULL) to check arguments status and if they have been checked using fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
1676
# end argument primary checking
Gael  MILLOT's avatar
Gael MILLOT committed
1677
# second round of checking and data preparation
1678
1679
# dealing with NA
if(any(is.na(fun)) | any(is.na(arg)) | any(is.na(expect.error)) | any(is.na(thread.nb)) | any(is.na(print.count)) | any(is.na(plot.fun)) | any(is.na(export)) | any(is.na(res.path)) | any(is.na(lib.path))){
1680
1681
tempo.cat <- paste0("ERROR IN ", function.name, ": NO ARGUMENT EXCEPT val CAN HAVE NA VALUES")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
1682
1683
1684
1685
}
# end dealing with NA
# dealing with NULL
if(is.null(fun) | is.null(arg) | is.null(val) | is.null(print.count) | is.null(plot.fun) | is.null(export)){
1686
1687
tempo.cat <- paste0("ERROR IN ", function.name, ": THESE ARGUMENTS\nfun\narg\nval\nprint.count\nplot.fun\nexport\nCANNOT BE NULL") #problematic arguments are -> put everywhere
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
1688
1689
}
# end dealing with NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1690
if(length(arg) != length(val)){
1691
tempo.cat <- paste0("ERROR IN ", function.name, ": LENGTH OF arg ARGUMENT MUST BE IDENTICAL TO LENGTH OF val ARGUMENT:\nHERE IT IS: ", length(arg), " VERSUS ", length(val))
Gael  MILLOT's avatar
Gael MILLOT committed
1692
1693
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
}
1694
args <- names(formals(get(fun))) # here no env = sys.nframe(), inherit = FALSE for get() because fun is a function in the classical scope
Gael  MILLOT's avatar
Gael MILLOT committed
1695
1696
if( ! all(arg %in% args)){
tempo.cat <- paste0("ERROR IN ", function.name, ": SOME OF THE STRINGS IN arg ARE NOT ARGUMENTS OF fun\nfun ARGUMENTS: ", paste(args, collapse = " "),"\nPROBLEMATIC STRINGS IN arg: ", paste(arg[ ! arg %in% args], collapse = " "))
1697
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1698
}
Gael  MILLOT's avatar
Gael MILLOT committed
1699
1700
if(sum(sapply(val, FUN = length) > 1) > 43){
tempo.cat <- paste0("ERROR IN ", function.name, ": CANNOT TEST MORE THAN 43 ARGUMENTS IF THEY ALL HAVE AT LEAST 2 VALUES EACH\nHERE THE NUMBER IS: ", sum(sapply(val, FUN = length) > 1))
1701
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1702
}
1703
1704
1705
1706
1707
1708
if( ! is.null(expect.error)){
if(length(val) != length(expect.error)){
tempo.cat <- paste0("ERROR IN ", function.name, ": LENGTH OF val ARGUMENT MUST BE IDENTICAL TO LENGTH OF expect.error ARGUMENT:\nHERE IT IS: ", length(val), " VERSUS ", length(expect.error))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1709
1710
1711
1712
1713
1714
1715
1716
1717
if( ! is.null(thread.nb) & is.null(res.path)){
tempo.cat <- paste0("ERROR IN ", function.name, ": res.path ARGUMENT MUST BE SPECIFIED IF thread.nb ARGUMENT IS NOT NULL")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
}
if(is.null(res.path) & export == TRUE){
tempo.cat <- paste0("ERROR IN ", function.name, ": res.path ARGUMENT MUST BE SPECIFIED IF export ARGUMENT TRUE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
}
if( ! is.null(thread.nb) & export == FALSE){
Gael  MILLOT's avatar
Gael MILLOT committed
1718
export <- TRUE
Gael  MILLOT's avatar
Gael MILLOT committed
1719
1720
1721
1722
tempo.cat <- paste0("WARNING FROM ", function.name, ": export ARGUMENT CONVERTED TO TRUE BECAUSE thread.nb ARGUMENT IS NOT NULL")
warning(paste0("\n", tempo.cat, "\n"), call. = FALSE)
}
# end second round of checking and data preparation
Gael  MILLOT's avatar
Gael MILLOT committed
1723
# package checking
Gael  MILLOT's avatar
Gael MILLOT committed
1724
fun_pack(req.package = c("lubridate"), lib.path = lib.path)
Gael  MILLOT's avatar
Gael MILLOT committed
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
if( ! is.null(thread.nb)){
fun_pack(req.package = c("parallel"), lib.path = lib.path)
}
# end package checking
# declaration of special plot functions
sp.plot.fun <- c("fun_gg_scatter", "fun_gg_bar", "fun_gg_boxplot")
# end declaration of special plot functions
# main code
cat("\nfun_test JOB IGNITION\n")
ini.date <- Sys.time()
ini.time <- as.numeric(ini.date) # time of process begin, converted into seconds
Gael  MILLOT's avatar
Gael MILLOT committed
1736
if(export == TRUE){
Gael  MILLOT's avatar
Gael MILLOT committed
1737
res.path <- paste0(res.path, "/fun_test_res_", trunc(ini.time))
Gael  MILLOT's avatar
Gael MILLOT committed
1738
if(dir.exists(res.path)){
1739
1740
tempo.cat <- paste0("ERROR IN ", function.name, ": FOLDER ALREADY EXISTS\n", res.path, "\nPLEASE RERUN ONCE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1741
1742
1743
1744
}else{
dir.create(res.path)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1745
1746
total.comp.nb <- prod(sapply(val, FUN = "length"))
cat(paste0("\nTHE TOTAL NUMBER OF TESTS IS: ", total.comp.nb, "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1747
# creation of the txt instruction that includes several loops
Gael  MILLOT's avatar
Gael MILLOT committed
1748
1749
1750
1751
loop.string <- NULL
end.loop.string <- NULL
fun.args <- NULL
fun.args2 <- NULL
1752
error.values <- NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1753
1754
arg.values <- "list("
for(i1 in 1:length(arg)){
Gael  MILLOT's avatar
Gael MILLOT committed
1755
if(is.null(thread.nb)){
Gael  MILLOT's avatar
Gael MILLOT committed
1756
if(length(val[[i1]]) > 1){ # loop only if more than one value in length(val[[i1]])
Gael  MILLOT's avatar
Gael MILLOT committed
1757
1758
loop.string <- paste0(loop.string, "for(i", i1, " in 1:", length(val[[i1]]), "){")
end.loop.string <- paste0(end.loop.string, "}")
Gael  MILLOT's avatar
Gael MILLOT committed
1759
}
Gael  MILLOT's avatar
Gael MILLOT committed
1760
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1761
loop.string <- "for(i in x){"
Gael  MILLOT's avatar
Gael MILLOT committed
1762
end.loop.string <- "}"
Gael  MILLOT's avatar
Gael MILLOT committed
1763
}
Gael  MILLOT's avatar
Gael MILLOT committed
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
fun.args <- paste0(
fun.args, 
ifelse(i1 == 1, "", ", "), 
arg[i1], 
" = val[[", 
i1, 
"]][[", 
if(is.null(thread.nb)){
if(length(val[[i1]]) > 1){
paste0("i", i1)
}else{
"1" # a unique element in val[[i1]]
}
}else{
paste0("i.list[[", i1, "]][i]")
}, 
"]]"
)
fun.args2 <- paste0(
fun.args2, 
ifelse(i1 == 1, "", ", "), 
arg[i1], 
" = val[[", 
i1, 
"]][[', ", 
if(is.null(thread.nb)){
if(length(val[[i1]]) > 1){
paste0("i", i1)
}else{
"1" # a unique element in val[[i1]]
}
}else{
paste0("i.list[[", i1, "]][i]")
}, 
", ']]"
)
arg.values <- paste0(
arg.values, 
"val[[", i1, "]][[", 
if(is.null(thread.nb)){
if(length(val[[i1]]) > 1){
paste0("i", i1)
}else{
"1" # a unique element in val[[i1]]
}
}else{
paste0("i.list[[", i1, "]][i]")
}, 
"]]", 
ifelse(i1 == length(arg), "", ", ")
)
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
error.values <- paste0(
error.values, 
ifelse(i1 == 1, "", " | "), 
"expect.error[[", i1, "]][[", 
if(is.null(thread.nb)){
if(length(expect.error[[i1]]) > 1){
paste0("i", i1)
}else{
"1" # a unique element in expect.error[[i1]]
}
}else{
paste0("i.list[[", i1, "]][i]")
}, 
"]]"
)
Gael  MILLOT's avatar
Gael MILLOT committed
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
}
arg.values <- paste0(arg.values, ")")
fun.test <- paste0(fun, "(", fun.args, ")")
fun.test2 <- paste0("paste0('", fun, "(", fun.args2, ")')")
# plot title for special plot functions
if(plot.fun == TRUE){
plot.kind <- "classic"
if(fun %in% sp.plot.fun){
plot.kind <- "special"
if(any(arg %in% "title")){ # this is for the special functions
tempo.match <- regmatches(x = fun.test, m = regexpr(text = fun.test, pattern = "title = .+[,)]"))
tempo.match <- substring(tempo.match , 1, nchar(tempo.match) - 1)
fun.test <- sub(x = fun.test, pattern = tempo.match, replacement = paste0(tempo.match, "\ntempo.title"))
}else{
fun.test <- sub(x = fun.test, pattern = ")$", replacement = ", title = tempo.title)")
}
}
}
# end plot title for special plot functions
kind <- character()
problem <- logical()
1851
expected.error <- logical()
Gael  MILLOT's avatar
Gael MILLOT committed
1852
1853
res <- character()
count <- 0
Gael  MILLOT's avatar
Gael MILLOT committed
1854
print.count.loop <- 0
Gael  MILLOT's avatar
Gael MILLOT committed
1855
plot.count <- 0
1856
1857
1858
1859
1860
if(length(arg) == 1){
data <- data.frame()
}else{ # length(arg) == 0 already tested above
data <- data.frame(t(vector("character", length(arg))), stringsAsFactors = FALSE)[-1, ] # -1 to remove the single row created and to have an empty data frame with length(arg) columns
}
Gael  MILLOT's avatar
Gael MILLOT committed
1861
1862
1863
code <- paste(
loop.string, '
count <- count + 1
Gael  MILLOT's avatar
Gael MILLOT committed
1864
print.count.loop <- print.count.loop + 1
Gael  MILLOT's avatar
Gael MILLOT committed
1865
data <- rbind(data, as.character(sapply(eval(parse(text = arg.values)), FUN = "paste", collapse = " ")), stringsAsFactors = FALSE) # each colum is a test
1866
1867
tempo.try.error <- fun_get_message(data = eval(parse(text = fun.test2)), kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE)) # data argument needs a character string but eval(parse(text = fun.test2)) provides it (eval parse replace the i1, i2, etc., by the correct values, meaning that only val is required in the env.name environment)
tempo.try.warning <- fun_get_message(data = eval(parse(text = fun.test2)), kind = "warning", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE), print.no = TRUE) # data argument needs a character string but eval(parse(text = fun.test2)) provides it (eval parse replace the i1, i2, etc., by the correct values, meaning that only val is required in the env.name environment)
1868
1869
1870
if( ! is.null(expect.error)){
expected.error <- c(expected.error, eval(parse(text = error.values)))
}
Gael  MILLOT's avatar
Gael MILLOT committed
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
if( ! is.null(tempo.try.error)){
kind <- c(kind, "ERROR")
problem <- c(problem, TRUE)
res <- c(res, tempo.try.error)
}else{
if( ! is.null(tempo.try.warning)){
kind <- c(kind, "WARNING")
problem <- c(problem, FALSE)
res <- c(res, tempo.try.warning)
}else{
kind <- c(kind, "OK")
problem <- c(problem, FALSE)
res <- c(res, "")
}
if(plot.fun == TRUE){
1886
invisible(dev.set(window.nb))
Gael  MILLOT's avatar
Gael MILLOT committed
1887
plot.count <- plot.count + 1
Gael  MILLOT's avatar
Gael MILLOT committed
1888
tempo.title <- paste0("test_", sprintf(paste0("%0", nchar(total.comp.nb), "d"), ifelse(is.null(thread.nb), count, x[count])))
Gael  MILLOT's avatar
Gael MILLOT committed
1889
1890
1891
1892
1893
1894
if(plot.kind == "classic"){
eval(parse(text = fun.test))
tempo <- fun_post_plot(corner.text = tempo.title)
}else if(plot.kind == "special"){
eval(parse(text = fun.test))
}else{
1895
1896
tempo.cat <- paste0("INTERNAL CODE ERROR 1 IN ", function.name, ": CODE HAS TO BE MODIFIED")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
1897
1898
1899
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1900
1901
if(print.count.loop == print.count){
print.count.loop <- 0
Gael  MILLOT's avatar
Gael MILLOT committed
1902
1903
tempo.time <- as.numeric(Sys.time())
tempo.lapse <- round(lubridate::seconds_to_period(tempo.time - ini.time))
1904
final.loop <- (tempo.time - ini.time) / count * ifelse(is.null(thread.nb), total.comp.nb, length(x)) # expected duration in seconds # intra nb.compar loop lapse: time lapse / cycles done * cycles remaining
Gael  MILLOT's avatar
Gael MILLOT committed
1905
1906
1907
final.exp <- as.POSIXct(final.loop, origin = ini.date)
cat(paste0(ifelse(is.null(thread.nb), "\n", paste0("\nIN PROCESS ", process.id, " | ")), "LOOP ", format(count, big.mark=","), " / ", format(ifelse(is.null(thread.nb), total.comp.nb, length(x)), big.mark=","), " | TIME SPENT: ", tempo.lapse, " | EXPECTED END: ", final.exp))
}
Gael  MILLOT's avatar
Gael MILLOT committed
1908
1909
1910
1911
if(count == ifelse(is.null(thread.nb), total.comp.nb, length(x))){
tempo.time <- as.numeric(Sys.time())
tempo.lapse <- round(lubridate::seconds_to_period(tempo.time - ini.time))
cat(paste0(ifelse(is.null(thread.nb), "\nLOOP PROCESS ENDED | ", paste0("\nPROCESS ", process.id, " ENDED | ")), "LOOP ", format(count, big.mark=","), " / ", format(ifelse(is.null(thread.nb), total.comp.nb, length(x)), big.mark=","), " | TIME SPENT: ", tempo.lapse, "\n\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1912
}
Gael  MILLOT's avatar
Gael MILLOT committed
1913
1914
1915
', 
end.loop.string
)
Gael  MILLOT's avatar
Gael MILLOT committed
1916
1917
1918
1919
# end creation of the txt instruction that includes several loops
if( ! is.null(thread.nb)){
# list of i numbers that will be split
i.list <- vector("list", length(val)) # positions to split in parallel jobs
1920
1921
1922
1923
1924
for(i2 in 1:length(arg)){
if(i2 == 1){
tempo.divisor <- total.comp.nb / length(val[[i2]])
i.list[[i2]] <- rep(1:length(val[[i2]]), each = as.integer(tempo.divisor))
tempo.multi <- length(val[[i2]])
Gael  MILLOT's avatar
Gael MILLOT committed
1925
}else{
1926
1927
1928
tempo.divisor <- tempo.divisor / length(val[[i2]])
i.list[[i2]] <- rep(rep(1:length(val[[i2]]), each = as.integer(tempo.divisor)), time = as.integer(tempo.multi))
tempo.multi <- tempo.multi * length(val[[i2]])
Gael  MILLOT's avatar
Gael MILLOT committed
1929
1930
1931
1932
1933
1934
1935
}
}
# end list of i numbers that will be split
tempo.cat <- paste0("PARALLELIZATION INITIATED AT: ", ini.date)
cat(paste0("\n", tempo.cat, "\n"))
tempo.thread.nb = parallel::detectCores(all.tests = FALSE, logical = TRUE) # detect the number of threads
if(tempo.thread.nb < thread.nb){
Gael  MILLOT's avatar
Gael MILLOT committed
1936
thread.nb <- tempo.thread.nb
Gael  MILLOT's avatar
Gael MILLOT committed
1937
1938
1939
}
tempo.cat <- paste0("NUMBER OF THREADS USED: ", thread.nb)
cat(paste0("\n    ", tempo.cat, "\n"))
1940
Clust <- parallel::makeCluster(thread.nb, outfile = paste0(res.path, "/fun_test_parall_log.txt")) # outfile to print or cat during parallelization (only possible in a file, outfile = "" do not work on windows)
1941
tempo.cat <- paste0("SPLIT OF TEST NUMBERS IN PARALLELISATION:")
Gael  MILLOT's avatar
Gael MILLOT committed
1942
cat(paste0("\n    ", tempo.cat, "\n"))
1943
1944
cluster.list <- parallel::clusterSplit(Clust, 1:total.comp.nb) # split according to the number of cluster
str(cluster.list) # using print(str()) add a NULL below the result
1945
cat("\n")
Gael  MILLOT's avatar
Gael MILLOT committed
1946
1947
paral.output.list <- parallel::clusterApply( # paral.output.list is a list made of thread.nb compartments, each made of n / thread.nb (mat theo column number) compartment. Each compartment receive the corresponding results of fun_permut(), i.e., data (permuted mat1.perm), warning message, cor (final correlation) and count (number of permutations)
cl = Clust,
1948
x = cluster.list,
Gael  MILLOT's avatar
Gael MILLOT committed
1949
function.name = function.name, 
1950
instruction = instruction, 
Gael  MILLOT's avatar
Gael MILLOT committed
1951
1952
thread.nb = thread.nb, 
print.count = print.count, 
Gael  MILLOT's avatar
Gael MILLOT committed
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
total.comp.nb = total.comp.nb, 
sp.plot.fun = sp.plot.fun,
i.list = i.list, 
fun.tested = fun,
arg.values = arg.values,
fun.test = fun.test,
fun.test2 = fun.test2,
kind = kind,
problem = problem,
res = res,
count = count,
plot.count = plot.count,
data = data,
code = code,
plot.fun = plot.fun, 
res.path = res.path, 
lib.path = lib.path, 
cute.path = cute.path, 
fun = function(
x, 
Gael  MILLOT's avatar
Gael MILLOT committed
1973
function.name, 
1974
instruction, 
Gael  MILLOT's avatar
Gael MILLOT committed
1975
1976
thread.nb, 
print.count, 
Gael  MILLOT's avatar
Gael MILLOT committed
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
total.comp.nb, 
sp.plot.fun, 
i.list, 
fun.tested, 
arg.values, 
fun.test, 
fun.test2, 
kind, 
problem, 
res, 
count, 
plot.count, 
data, 
code, 
plot.fun, 
res.path, 
lib.path, 
cute.path
){
# check again: very important because another R
Gael  MILLOT's avatar
Gael MILLOT committed
1997
1998
process.id <- Sys.getpid()
cat(paste0("\nPROCESS ID ", process.id, " -> TESTS ", x[1], " TO ", x[length(x)], "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
1999
2000
source(cute.path, local = .GlobalEnv)
fun_pack(req.package = "lubridate", lib.path = lib.path, load = TRUE) # load = TRUE to be sure that functions are present in the environment. And this prevent to use R.lib.path argument of fun_python_pack()