cute_little_R_functions.R 581 KB
Newer Older
Gael  MILLOT's avatar
Gael MILLOT committed
1
2
3
4
5
6
7
8
9
################################################################
##                                                            ##
##     CUTE FUNCTIONS v6.0.0                                  ##
##                                                            ##
##     Gael A. Millot                                         ##
##                                                            ##
##     Compatible with R v3.6.1                               ##
##                                                            ##
################################################################
Gael  MILLOT's avatar
Gael MILLOT committed
10
11


Gael  MILLOT's avatar
Gael MILLOT committed
12
13
14
# https://usethis.r-lib.org/ and usethat also
# BEWARE: do not forget to save the modifications in the .R file (through RSTUDIO for indentation)
# update graphic examples with good comment, as in barplot
15
#is there any interest to be able to source the cute file elsewhere than in global env? If yes, but may be interesting to put it into a new environement just above .GlobalEnv environment. See https://stackoverflow.com/questions/9002544/how-to-add-functions-in-an-existing-environment
Gael  MILLOT's avatar
Gael MILLOT committed
16
# Make a first round check for each function if required
Gael  MILLOT's avatar
Gael MILLOT committed
17
# Update all argument description, saying, character vector, etc.
Gael  MILLOT's avatar
Gael MILLOT committed
18
# check all the functions using fun_test
Gael  MILLOT's avatar
Gael MILLOT committed
19
20
21
22
23
# Templates: https://prettydoc.statr.me/themes.html
# # package: http://r-pkgs.had.co.nz/
# https://pkgdown.r-lib.org/
# https://rdrr.io/github/gastonstat/cointoss/
# doc:https://www.sphinx-doc.org/en/master/man/sphinx-autogen.html considering that https://www.ericholscher.com/blog/2014/feb/11/sphinx-isnt-just-for-python/
Gael  MILLOT's avatar
Gael MILLOT committed
24
25
26
27
# https://docs.readthedocs.io/en/stable/intro/getting-started-with-sphinx.html
# https://docs.gitlab.com/ee/user/project/pages/
# also register into biotools

Gael  MILLOT's avatar
Gael MILLOT committed
28

Gael  MILLOT's avatar
Gael MILLOT committed
29
################################ OUTLINE ################################
Gael  MILLOT's avatar
Gael MILLOT committed
30
31


Gael  MILLOT's avatar
Gael MILLOT committed
32
33
################ Object analysis    2
######## fun_check() #### check class, type, length, etc., of objects   2
Gael  MILLOT's avatar
tempo    
Gael MILLOT committed
34
35
36
######## fun_info() #### recover object information 9
######## fun_head() #### head of the left or right of big 2D objects    11
######## fun_tail() #### tail of the left or right of big 2D objects    12
Gael  MILLOT's avatar
Gael MILLOT committed
37
38
######## fun_comp_1d() #### comparison of two 1D datasets (vectors, factors, 1D tables) 13
######## fun_comp_2d() #### comparison of two 2D datasets (row & col names, dimensions, etc.)   17
Gael  MILLOT's avatar
tempo    
Gael MILLOT committed
39
40
######## fun_comp_list() #### comparison of two lists   24
######## fun_test() #### test combinations of argument values of a function 26
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
################ Object modification    40
######## fun_name_change() #### check a vector of character strings and modify any string if present in another vector  40
######## fun_df_remod() #### remodeling a data frame to have column name as a qualitative values and vice-versa 42
######## fun_merge() #### merge the columns of two 2D objects, by common rows   45
######## fun_round() #### rounding number if decimal present    49
######## fun_mat_rotate() #### 90° clockwise matrix rotation    51
######## fun_mat_num2color() #### convert a numeric matrix into hexadecimal color matrix    52
######## fun_mat_op() #### assemble several matrices with operation 55
######## fun_mat_inv() #### return the inverse of a square matrix   57
######## fun_mat_fill() #### fill the empty half part of a symmetric square matrix  58
######## fun_permut() #### progressively breaks a vector order  62
################ Graphics management    72
######## fun_width() #### window width depending on classes to plot 73
######## fun_open() #### open a GUI or pdf graphic window   74
######## fun_prior_plot() #### set graph param before plotting (erase axes for instance)    78
######## fun_scale() #### select nice label numbers when setting number of ticks on an axis 82
######## fun_inter_ticks() #### define coordinates of secondary ticks for log scales    87
######## fun_post_plot() #### set graph param after plotting (axes redesign for instance)   89
######## fun_close() #### close specific graphic windows    101
################ Standard graphics  102
######## fun_empty_graph() #### text to display for empty graphs    102
################ gg graphics    104
######## fun_gg_palette() #### ggplot2 default color palette    104
######## fun_gg_just() #### ggplot2 justification of the axis labeling, depending on angle  106
######## fun_gg_point_rast() #### ggplot2 raster scatterplot layer  108
######## fun_gg_scatter() #### ggplot2 scatterplot + lines (up to 6 overlays totally)   112
######## fun_gg_bar() #### ggplot2 mean barplot + overlaid dots if required 112
######## fun_gg_boxplot() #### ggplot2 boxplot + background dots if required    112
######## fun_gg_prop() #### ggplot2 proportion barplot  112
######## fun_gg_dot() #### ggplot2 categorial dotplot + mean/median 112
######## fun_gg_violin() #### ggplot2 violins   112
######## fun_gg_line() #### ggplot2 lines + background dots and error bars  112
######## fun_gg_empty_graph() #### text to display for empty graphs 112
################ Graphic extraction 114
######## fun_trim() #### display values from a quantitative variable and trim according to defined cut-offs 114
######## fun_segmentation() #### segment a dot cloud on a scatterplot and define the dots from another cloud outside the segmentation   122
################ Import 155
######## fun_pack() #### check if R packages are present and import into the working environment    155
######## fun_python_pack() #### check if python packages are present    156
################ Print / Exporting results (text & tables)  159
######## fun_report() #### print string or data object into output file 159
######## fun_get_message() #### return messages of an expression (that can be exported) 162
Gael  MILLOT's avatar
Gael MILLOT committed
83
84


Gael  MILLOT's avatar
Gael MILLOT committed
85
################################ FUNCTIONS ################################
Gael  MILLOT's avatar
Gael MILLOT committed
86

Gael  MILLOT's avatar
Gael MILLOT committed
87
88
89
90
91
92
93

################ Object analysis


######## fun_check() #### check class, type, length, etc., of objects


94
# Check r_debugging_tools-v1.2.R OK
95
96
# Check fun_test() (see cute_checks.docx) Ok
# check manual: example to scan again
97
# clear to go Apollo
Gael  MILLOT's avatar
Gael MILLOT committed
98
99
100
101
102
fun_check <- function(data, data.name = NULL, class = NULL, typeof = NULL, mode = NULL, length = NULL, prop = FALSE, double.as.integer.allowed = FALSE, options = NULL, all.options.in.data = FALSE, na.contain = FALSE, neg.values = TRUE, print = FALSE, fun.name = NULL){
# AIM
# check the class, type, mode and length of the data argument
# mainly used to check the arguments of other functions
# check also other kind of data parameters, is it a proportion? Is it type double but numbers without decimal part?
103
# if options == NULL, then at least class or type or mode or length argument must be non null
Gael  MILLOT's avatar
Gael MILLOT committed
104
105
106
107
108
# if options is non null, then class, type and mode must be NULL, and length can be NULL or specified
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
# ARGUMENTS
# data: object to test
109
110
111
# data.name: character string indicating the name of the object to test. If NULL, use the name of the object assigned to the data argument
# class: character string. Either one of the class() result or "vector" or NULL
# typeof: character string. Either one of the typeof() result or NULL
112
# mode: character string. Either one of the mode() result (for non vector object) or NULL
113
114
115
116
# length: numeric value indicating the length of the object. Not considered if NULL
# prop: logical. Are the numeric values between 0 and 1 (proportion)? If TRUE, can be used alone, without considering class, etc.
# double.as.integer.allowed: logical. If TRUE, no error is reported if argument is set to typeof == "integer" or class == "integer", while the reality is typeof == "double" or class == "numeric" but the numbers have a zero as modulo (remainder of a division). This means that i <- 1 , which is typeof(i) -> "double" is considered as integer with double.as.integer.allowed = TRUE
# options: a vector of character strings indicating all the possible option values for data
117
# all.options.in.data: logical. If TRUE, all of the options must be present at least once in data, and nothing else. If FALSE, some or all of the options must be present in data, and nothing else. Ignored if options is NULL
118
# na.contain: logical. Can data contain NA?
119
# neg.values: logical. Are negative numeric values authorized? BEWARE: only considered if set to FALSE, to check for non negative values when class is set to "vector", "numeric", "matrix", "array", "data.frame", "table", or typeof is set to "double", "integer", or mode is set to "numeric". Ignored in other cases, notably with prop argument
120
# print: logical. Print the error message if $problem is TRUE? See the example section
121
# fun.name: character string indicating the name of the function checked (i.e., when fun_check() is used to check its argument). If non NULL, name will be added into the error message returned by fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
122
123
# RETURN
# a list containing:
124
# $problem: logical. Is there any problem detected?
Gael  MILLOT's avatar
Gael MILLOT committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# $text: the problem detected
# $fun.name: name of the checked parameter
# EXAMPLES
# test <- 1:3 ; fun_check(data = test, data.name = NULL, print = TRUE, options = NULL, all.options.in.data = FALSE, class = NULL, typeof = NULL, mode = NULL, prop = TRUE, double.as.integer.allowed = FALSE, length = NULL)
# test <- 1:3 ; fun_check(data = test, print = TRUE, class = "numeric", typeof = NULL, double.as.integer.allowed = FALSE)
# test <- 1:3 ; fun_check(data = test, print = TRUE, class = "vector", mode = "numeric")
# argument print with and without assignation
# test <- 1:3 ; tempo <- fun_check(data = test, print = TRUE, class = "vector", mode = "character")
# test <- 1:3 ; tempo <- fun_check(data = test, print = FALSE, class = "vector", mode = "character") # the assignation allows to recover a problem without printing it
# test <- 1:3 ; fun_check(data = test, print = TRUE, class = "vector", mode = "character")
# test <- matrix(1:3) ; fun_check(data = test, print = TRUE, class = "vector", mode = "numeric")
# DEBUGGING
# data = expression(TEST) ; data.name = NULL ; class = "vector" ; typeof = NULL ; mode = NULL ; length = 1 ; prop = FALSE ; double.as.integer.allowed = FALSE ; options = NULL ; all.options.in.data = FALSE ; na.contain = FALSE ; neg.values = TRUE ; print = TRUE ; fun.name = NULL
# function name: no used in this function for the error message, to avoid env colliding
# argument checking
140
141
142
143
144
145
146
147
# fun.name checked first because required next
if( ! is.null(fun.name)){
if( ! (class(fun.name) == "character" & length(fun.name) == 1)){
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check(): THE fun.name ARGUMENT MUST BE A CHARACTER VECTOR OF LENGTH 1: ", paste(fun.name, collapse = " "), "\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
}
# end fun.name checked first because required next
148
149
# arg with no default values
if(missing(data)){
150
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": ARGUMENT data HAS NO DEFAULT VALUE AND REQUIRES ONE\n\n================\n\n")
151
152
153
stop(tempo.cat, call. = FALSE)
}
# end arg with no default values
154
155
# dealing with NA
if(any(is.na(data.name)) | any(is.na(class)) | any(is.na(typeof)) | any(is.na(mode)) | any(is.na(length)) | any(is.na(prop)) | any(is.na(double.as.integer.allowed)) | any(is.na(all.options.in.data)) | any(is.na(na.contain)) | any(is.na(neg.values)) | any(is.na(print)) | any(is.na(fun.name))){
156
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": NO ARGUMENT EXCEPT data AND options CAN HAVE NA VALUES\nPROBLEMATIC ARGUMENTS ARE: ", paste(c("data.name", "class", "typeof", "mode", "length", "prop", "double.as.integer.allowed", "all.options.in.data", "na.contain", "neg.values", "print", "fun.name")[c(any(is.na(data.name)), any(is.na(class)), any(is.na(typeof)), any(is.na(mode)), any(is.na(length)), any(is.na(prop)), any(is.na(double.as.integer.allowed)), any(is.na(all.options.in.data)), any(is.na(na.contain)), any(is.na(neg.values)), any(is.na(print)), any(is.na(fun.name)))], collapse = " "), "\n\n================\n\n")
157
158
159
160
stop(tempo.cat, call. = FALSE)
}
# end dealing with NA
# dealing with NULL
161
if(is.null(prop) | is.null(double.as.integer.allowed) | is.null(all.options.in.data) | is.null(na.contain) | is.null(neg.values) | is.null(print)){
162
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THESE ARGUMENTS prop, double.as.integer.allowed, all.options.in.data, na.contain, neg.values AND print CANNOT BE NULL\nPROBLEMATIC ARGUMENTS ARE: ", paste(c("prop", "double.as.integer.allowed", "all.options.in.data", "na.contain", "neg.values", "print")[c(is.null(prop), is.null(double.as.integer.allowed), is.null(all.options.in.data), is.null(na.contain), is.null(neg.values), is.null(print))], collapse = " "), "\n\n================\n\n")
163
164
165
166
167
168
stop(tempo.cat, call. = FALSE)
}
# end dealing with NULL
# dealing with logical
# tested below
# end dealing with logical
Gael  MILLOT's avatar
Gael MILLOT committed
169
170
if( ! is.null(data.name)){
if( ! (length(data.name) == 1 & class(data.name) == "character")){
171
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": data.name ARGUMENT MUST BE A SINGLE CHARACTER ELEMENT AND NOT ", paste(data.name, collapse = " "), "\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
172
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
173
}
Gael  MILLOT's avatar
Gael MILLOT committed
174
}
Gael  MILLOT's avatar
Gael MILLOT committed
175
if(is.null(options) & is.null(class) & is.null(typeof) & is.null(mode) &  prop == FALSE & is.null(length)){
176
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": AT LEAST ONE OF THE options, class, typeof, mode, prop, OR length ARGUMENT MUST BE SPECIFIED (I.E, TRUE FOR prop)\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
177
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
178
}
Gael  MILLOT's avatar
Gael MILLOT committed
179
if( ! is.null(options) & ( ! is.null(class) | ! is.null(typeof) | ! is.null(mode) | prop == TRUE)){
180
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THE class, typeof, mode ARGUMENTS MUST BE NULL, AND prop FALSE, IF THE options ARGUMENT IS SPECIFIED\nTHE options ARGUMENT MUST BE NULL IF THE class AND/OR typeof AND/OR mode AND/OR prop ARGUMENT IS SPECIFIED\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
181
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
182
}
Gael  MILLOT's avatar
Gael MILLOT committed
183
if( ! (all(class(neg.values) == "logical") & length(neg.values) == 1 & any(is.na(neg.values)) != TRUE)){
184
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THE neg.values ARGUMENT MUST BE TRUE OR FALSE ONLY\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
185
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
186
}
Gael  MILLOT's avatar
Gael MILLOT committed
187
if(neg.values == FALSE & is.null(class) & is.null(typeof) & is.null(mode)){
188
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THE neg.values ARGUMENT CANNOT BE SWITCHED TO FALSE IF class, typeof AND mode ARGUMENTS ARE NULL\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
189
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
190
}
Gael  MILLOT's avatar
Gael MILLOT committed
191
192
if( ! is.null(class)){
if( ! all(class %in% c("vector", "logical", "integer", "numeric", "complex", "character", "matrix", "array", "data.frame", "list", "factor", "table", "expression", "name", "symbol", "function", "uneval", "environment") & any(is.na(class)) != TRUE)){ # not length == 1 here because ordered factors are class "factor" "ordered" (length == 2)
193
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": class ARGUMENT MUST BE ONE OF THESE VALUE:\n\"vector\", \"logical\", \"integer\", \"numeric\", \"complex\", \"character\", \"matrix\", \"array\", \"data.frame\", \"list\", \"factor\", \"table\", \"expression\", \"name\", \"symbol\", \"function\", \"environment\"\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
194
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
195
}
Gael  MILLOT's avatar
Gael MILLOT committed
196
if(neg.values == FALSE & ! any(class %in% c("vector", "numeric", "integer", "matrix", "array", "data.frame", "table"))){
197
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": class ARGUMENT CANNOT BE OTHER THAN \"vector\", \"numeric\", \"integer\", \"matrix\", \"array\", \"data.frame\", \"table\" IF neg.values ARGUMENT IS SWITCHED TO FALSE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
198
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
199
200
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
201
if( ! is.null(typeof)){
202
203
if( ! (all(typeof %in% c("logical", "integer", "double", "complex", "character", "list", "expression", "name", "symbol", "closure", "special", "builtin", "environment", "S4")) & length(typeof) == 1 & any(is.na(typeof)) != TRUE)){
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": typeof ARGUMENT MUST BE ONE OF THESE VALUE:\n\"logical\", \"integer\", \"double\", \"complex\", \"character\", \"list\", \"expression\", \"name\", \"symbol\", \"closure\", \"special\", \"builtin\", \"environment\", \"S4\"\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
204
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
205
}
Gael  MILLOT's avatar
Gael MILLOT committed
206
if(neg.values == FALSE & ! typeof %in% c("double", "integer")){
207
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": typeof ARGUMENT CANNOT BE OTHER THAN \"double\" OR \"integer\" IF neg.values ARGUMENT IS SWITCHED TO FALSE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
208
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
209
210
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
211
if( ! is.null(mode)){
212
213
if( ! (all(mode %in% c("logical", "numeric", "complex", "character", "list", "expression", "name", "symbol", "function", "environment", "S4")) & length(mode) == 1 & any(is.na(mode)) != TRUE)){
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": mode ARGUMENT MUST BE ONE OF THESE VALUE:\n\"logical\", \"numeric\", \"complex\", \"character\", \"list\", \"expression\", \"name\", \"symbol\", \"function\", \"environment\", \"S4\"\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
214
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
215
}
Gael  MILLOT's avatar
Gael MILLOT committed
216
if(neg.values == FALSE & mode != "numeric"){
217
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": mode ARGUMENT CANNOT BE OTHER THAN \"numeric\" IF neg.values ARGUMENT IS SWITCHED TO FALSE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
218
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
219
220
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
221
222
if( ! is.null(length)){
if( ! (is.numeric(length) & length(length) == 1 & ! grepl(length, pattern = "\\.") & any(is.na(length)) != TRUE)){
223
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": length ARGUMENT MUST BE A SINGLE INTEGER VALUE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
224
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
225
226
}
}
227
if( ! (is.logical(prop) | (length(prop) == 1 & any(is.na(prop)) != TRUE))){
228
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": prop ARGUMENT MUST BE TRUE OR FALSE ONLY\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
229
230
231
stop(tempo.cat, call. = FALSE)
}else if(prop == TRUE){
if( ! is.null(class)){
232
if( ! any(class %in% c("vector", "numeric", "matrix", "array", "data.frame", "table"))){
233
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": class ARGUMENT CANNOT BE OTHER THAN NULL, \"vector\", \"numeric\", \"matrix\", \"array\", \"data.frame\", \"table\" IF prop ARGUMENT IS TRUE\n\n================\n\n") # not integer because prop
Gael  MILLOT's avatar
Gael MILLOT committed
234
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
235
236
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
237
238
if( ! is.null(mode)){
if(mode != "numeric"){
239
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": mode ARGUMENT CANNOT BE OTHER THAN NULL OR \"numeric\" IF prop ARGUMENT IS TRUE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
240
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
241
242
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
243
244
if( ! is.null(typeof)){
if(typeof != "double"){
245
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": typeof ARGUMENT CANNOT BE OTHER THAN NULL OR \"double\" IF prop ARGUMENT IS TRUE\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
246
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
247
248
249
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
250
if( ! (all(class(double.as.integer.allowed) == "logical") & length(double.as.integer.allowed) == 1 & any(is.na(double.as.integer.allowed)) != TRUE)){
251
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THE double.as.integer.allowed ARGUMENT MUST BE TRUE OR FALSE ONLY: ", paste(double.as.integer.allowed, collapse = " "), "\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
252
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
253
}
Gael  MILLOT's avatar
Gael MILLOT committed
254
if( ! (is.logical(all.options.in.data) & length(all.options.in.data) == 1 & any(is.na(all.options.in.data)) != TRUE)){
255
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": all.options.in.data ARGUMENT MUST BE A SINGLE LOGICAL VALUE (TRUE OR FALSE ONLY): ", paste(all.options.in.data, collapse = " "), "\n\n================\n\n")
Gael  MILLOT's avatar
Gael MILLOT committed
256
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
257
}
Gael  MILLOT's avatar
Gael MILLOT committed
258
259
260
if( ! (all(class(na.contain) == "logical") & length(na.contain) == 1 & any(is.na(na.contain)) != TRUE)){
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check(): THE na.contain ARGUMENT MUST BE TRUE OR FALSE ONLY: ", paste(na.contain, collapse = " "), "\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
261
}
Gael  MILLOT's avatar
Gael MILLOT committed
262
263
264
if( ! (all(class(print) == "logical") & length(print) == 1 & any(is.na(print)) != TRUE)){
tempo.cat <- paste0("\n\n================\n\nERROR IN fun_check(): THE print ARGUMENT MUST BE TRUE OR FALSE ONLY: ", paste(print, collapse = " "), "\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
265
}
266
# fun.name tested at the beginning
Gael  MILLOT's avatar
Gael MILLOT committed
267
268
269
270
271
272
273
274
275
276
277
278
# 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
if(is.null(data.name)){
data.name <- deparse(substitute(data))
}
problem <- FALSE
text <- paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER")
if( ! is.null(options)){
text <- ""
if( ! all(data %in% options)){
problem <- TRUE
279
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " PARAMETER MUST BE SOME OF THESE OPTIONS: ", paste(options, collapse = " "), "\nTHE PROBLEMATIC ELEMENTS OF data ARE: ", paste(unique(data[ ! (data %in% options)]), collapse = " "))
Gael  MILLOT's avatar
Gael MILLOT committed
280
281
282
283
}
if(all.options.in.data == TRUE){
if( ! all(options %in% data)){
problem <- TRUE
284
text <- paste0(ifelse(text == "", "", paste0(text, "\n")), ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " PARAMETER MUST BE MADE OF ALL THESE OPTIONS: ", paste(options, collapse = " "), "\nTHE MISSING ELEMENTS OF THE options ARGUMENT ARE: ",  paste(unique(options[ ! (options %in% data)]), collapse = " "))
Gael  MILLOT's avatar
Gael MILLOT committed
285
}
Gael  MILLOT's avatar
Gael MILLOT committed
286
}
Gael  MILLOT's avatar
Gael MILLOT committed
287
288
289
if( ! is.null(length)){
if(length(data) != length){
problem <- TRUE
290
text <- paste0(ifelse(text == "", "", paste0(text, "\n")), ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE LENGTH OF ", data.name, " MUST BE ", length, " AND NOT ", length(data))
Gael  MILLOT's avatar
Gael MILLOT committed
291
292
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
293
294
if(text == ""){
text <- paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER")
Gael  MILLOT's avatar
Gael MILLOT committed
295
}
Gael  MILLOT's avatar
Gael MILLOT committed
296
}
Gael  MILLOT's avatar
Gael MILLOT committed
297
298
299
300
301
302
303
304
305
arg.names <- c("class", "typeof", "mode", "length")
if(is.null(options)){
for(i2 in 1:length(arg.names)){
if( ! is.null(get(arg.names[i2]))){
# script to execute
tempo.script <- '
problem <- TRUE ;
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " PARAMETER MUST BE ") ;
Gael  MILLOT's avatar
Gael MILLOT committed
306
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
307
text <- paste0(text, " AND ") ; 
Gael  MILLOT's avatar
Gael MILLOT committed
308
}
Gael  MILLOT's avatar
Gael MILLOT committed
309
310
311
312
313
314
text <- paste0(text, toupper(arg.names[i2]), " ", get(arg.names[i2]))
'
# end script to execute
if(typeof(data) == "double" & double.as.integer.allowed == TRUE & ((arg.names[i2] == "class" & get(arg.names[i2]) == "integer") | (arg.names[i2] == "typeof" & get(arg.names[i2]) == "integer"))){
if( ! all(data%%1 == 0)){ # to check integers (use %%, meaning the remaining of a division): see the precedent line. isTRUE(all.equal(data%%1, rep(0, length(data)))) not used because we strictly need zero as a result
eval(parse(text = tempo.script)) # execute tempo.script
Gael  MILLOT's avatar
Gael MILLOT committed
315
}
Gael  MILLOT's avatar
Gael MILLOT committed
316
317
318
319
}else if(get(arg.names[i2]) != "vector" & eval(parse(text = paste0(arg.names[i2], "(data)"))) != get(arg.names[i2])){
eval(parse(text = tempo.script)) # execute tempo.script
}else if(arg.names[i2] == "class" & get(arg.names[i2]) == "vector" & ! (class(data) == "numeric" | class(data) == "integer" | class(data) == "character" | class(data) == "logical")){
eval(parse(text = tempo.script)) # execute tempo.script
Gael  MILLOT's avatar
Gael MILLOT committed
320
321
322
323
}
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
324
if(prop == TRUE){
Gael  MILLOT's avatar
Gael MILLOT committed
325
if(is.null(data) | any(data < 0 | data > 1, na.rm = TRUE)){
Gael  MILLOT's avatar
Gael MILLOT committed
326
327
328
329
330
problem <- TRUE
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
Gael  MILLOT's avatar
Gael MILLOT committed
331
}
Gael  MILLOT's avatar
Gael MILLOT committed
332
text <- paste0(text, "THE ", data.name, " PARAMETER MUST BE DECIMAL VALUES BETWEEN 0 AND 1")
Gael  MILLOT's avatar
Gael MILLOT committed
333
}
334
}
Gael  MILLOT's avatar
Gael MILLOT committed
335
336
if(all(class(data) %in% "expression")){
data <- as.character(data) # to evaluate the presence of NA
Gael  MILLOT's avatar
Gael MILLOT committed
337
}
338
if(na.contain == FALSE & (mode(data) %in% c("logical", "numeric", "complex", "character", "list", "expression", "name", "symbol"))){ # before it was ! (class(data) %in% c("function", "environment"))
Gael  MILLOT's avatar
Gael MILLOT committed
339
340
341
342
if(any(is.na(data)) == TRUE){ # not on the same line because when data is class envir or function , do not like that
problem <- TRUE
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
Gael  MILLOT's avatar
Gael MILLOT committed
343
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
344
text <- paste0(text, " AND ")
Gael  MILLOT's avatar
Gael MILLOT committed
345
}
Gael  MILLOT's avatar
Gael MILLOT committed
346
text <- paste0(text, "THE ", data.name, " PARAMETER CONTAINS NA WHILE NOT AUTHORIZED")
Gael  MILLOT's avatar
Gael MILLOT committed
347
}
Gael  MILLOT's avatar
Gael MILLOT committed
348
}
Gael  MILLOT's avatar
Gael MILLOT committed
349
350
351
352
353
if(neg.values == FALSE){
if(any(data < 0, na.rm = TRUE)){
problem <- TRUE
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " PARAMETER"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
Gael  MILLOT's avatar
Gael MILLOT committed
354
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
355
text <- paste0(text, " AND ")
Gael  MILLOT's avatar
Gael MILLOT committed
356
}
Gael  MILLOT's avatar
Gael MILLOT committed
357
text <- paste0(text, "THE ", data.name, " PARAMETER MUST BE NON NEGATIVE NUMERIC VALUES")
Gael  MILLOT's avatar
Gael MILLOT committed
358
359
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
360
361
if(print == TRUE & problem == TRUE){
cat(paste0("\n\n================\n\n", text, "\n\n================\n\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
362
}
Gael  MILLOT's avatar
Gael MILLOT committed
363
364
output <- list(problem = problem, text = text, fun.name = data.name)
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
365
}
Gael  MILLOT's avatar
Gael MILLOT committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449


######## fun_info() #### recover object information


# Check OK: clear to go Apollo
fun_info <- function(data){
# AIM
# provide a full description of an object
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
# ARGUMENTS
# data: object to test
# RETURN
# a list containing information, depending on the class and type of data
# if data is made of numerics, provide range, sum, mean, number of NA and number of Inf
# please, use names(fun_info()) and remove what can be too big for easy analysis
# EXAMPLES
# fun_info(data = 1:3)
# fun_info(data.frame(a = 1:2, b = ordered(factor(c("A", "B")))))
# fun_info(list(a = 1:3, b = ordered(factor(c("A", "B")))))
# DEBUGGING
# data = NULL # for function debugging
# data = 1:3 # for function debugging
# data = matrix(1:3) # for function debugging
# data = data.frame(a = 1:2, b = c("A", "B")) # for function debugging
# data = factor(c("b", "a")) # for function debugging
# data = ordered(factor(c("b", "a"))) # for function debugging
# data = list(a = 1:3, b = factor(c("A", "B"))) # for function debugging
# data = list(a = 1:3, b = ordered(factor(c("A", "B")))) # for function debugging
# function name: no need because no check and no message
# argument checking
# 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
data.name <- deparse(substitute(data))
output <- list("NAME" = data.name)
tempo <- list("CLASS" = class(data))
output <- c(output, tempo)
tempo <- list("TYPE" = typeof(data))
output <- c(output, tempo)
tempo <- list("LENGTH" = length(data))
output <- c(output, tempo)
if(all(typeof(data) %in% c("integer", "numeric", "double"))){
tempo <- list("RANGE" = range(data[ ! is.infinite(data)], na.rm = TRUE))
output <- c(output, tempo)
tempo <- list("SUM" = sum(data[ ! is.infinite(data)], na.rm = TRUE))
output <- c(output, tempo)
tempo <- list("MEAN" = mean(data[ ! is.infinite(data)], na.rm = TRUE))
output <- c(output, tempo)
tempo <- list("NA.NB" = sum(is.na(data)))
output <- c(output, tempo)
tempo <- list("INF.NB" = sum(is.infinite(data)))
output <- c(output, tempo)
}
tempo <- list("HEAD" = head(data))
output <- c(output, tempo)
if( ! is.null(data)){
tempo <- list("TAIL" = tail(data))
output <- c(output, tempo)
if( ! is.null(dim(data))){
tempo <- list("DIMENSION" = dim(data))
names(tempo[[1]]) <- c("NROW", "NCOL")
output <- c(output, tempo)
}
tempo <- list("SUMMARY" = summary(data))
output <- c(output, tempo)
}
if(all(class(data) == "data.frame" | class(data) == "matrix")){
tempo <- list("ROW_NAMES" = dimnames(data)[[1]])
output <- c(output, tempo)
tempo <- list("COLUM_NAMES" = dimnames(data)[[2]])
output <- c(output, tempo)
}
if(all(class(data) == "data.frame")){
tempo <- list("STRUCTURE" = ls.str(data)) # str() print automatically, ls.str() not but does not give the order of the data.frame
output <- c(output, tempo)
tempo <- list("COLUMN_TYPE" = sapply(data, FUN = "typeof"))
if(any(sapply(data, FUN = "class") %in% "factor")){ # if an ordered factor is present, then sapply(data, FUN = "class") return a list but works with any(sapply(data, FUN = "class") %in% "factor") 
tempo.class <- sapply(data, FUN = "class")
if(any(unlist(tempo.class) %in% "ordered")){
tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor"
}else{
tempo2 <- unlist(tempo.class)
Gael  MILLOT's avatar
Gael MILLOT committed
450
}
Gael  MILLOT's avatar
Gael MILLOT committed
451
tempo[["COLUMN_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
Gael  MILLOT's avatar
Gael MILLOT committed
452
}
Gael  MILLOT's avatar
Gael MILLOT committed
453
output <- c(output, tempo)
Gael  MILLOT's avatar
Gael MILLOT committed
454
}
Gael  MILLOT's avatar
Gael MILLOT committed
455
456
457
458
459
460
461
462
if(all(class(data) == "list")){
tempo <- list("COMPARTMENT_NAMES" = names(data))
output <- c(output, tempo)
tempo <- list("COMPARTMENT_TYPE" = sapply(data, FUN = "typeof"))
if(any(unlist(sapply(data, FUN = "class")) %in% "factor")){ # if an ordered factor is present, then sapply(data, FUN = "class") return a list but works with any(sapply(data, FUN = "class") %in% "factor") 
tempo.class <- sapply(data, FUN = "class")
if(any(unlist(tempo.class) %in% "ordered")){
tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor"
Gael  MILLOT's avatar
Gael MILLOT committed
463
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
464
tempo2 <- unlist(tempo.class)
Gael  MILLOT's avatar
Gael MILLOT committed
465
}
Gael  MILLOT's avatar
Gael MILLOT committed
466
tempo[["COMPARTMENT_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
Gael  MILLOT's avatar
Gael MILLOT committed
467
}
Gael  MILLOT's avatar
Gael MILLOT committed
468
output <- c(output, tempo)
Gael  MILLOT's avatar
Gael MILLOT committed
469
}
Gael  MILLOT's avatar
Gael MILLOT committed
470
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
471
}
Gael  MILLOT's avatar
Gael MILLOT committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502


######## fun_head() #### head of the left or right of big 2D objects


# Check OK: clear to go Apollo
fun_head <- function(data1, n = 6, side = "l"){
# AIM
# as head() but display the left or right head of big 2D objects
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# ARGUMENTS
# data1: any object but more dedicated for matrix, data frame or table
# n: as in head() but for for matrix, data frame or table, number of dimension to print (10 means 10 rows and columns)
# side: either "l" or "r" for the left or right side of the 2D object (only for matrix, data frame or table)
# BEWARE: other arguments of head() not used
# RETURN
# the head
# EXAMPLES
# obs1 = matrix(1:30, ncol = 5, dimnames = list(letters[1:6], LETTERS[1:5])) ; obs1 ; fun_head(obs1, 3)
# obs1 = matrix(1:30, ncol = 5, dimnames = list(letters[1:6], LETTERS[1:5])) ; obs1 ; fun_head(obs1, 3, "right")
# DEBUGGING
# data1 = matrix(1:30, ncol = 5) # for function debugging
# data1 = matrix(1:30, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# function name
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
503
}
Gael  MILLOT's avatar
Gael MILLOT committed
504
505
506
507
508
509
510
511
512
513
# end required function checking
# argument checking
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 = n, class = "vector", typeof = "integer", double.as.integer.allowed = TRUE, length = 1, fun.name = function.name) ; eval(ee)
tempo <- fun_check(data = side, options = c("l", "r"), length = 1, fun.name = function.name) ; eval(ee)
if(any(arg.check) == TRUE){
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) #
Gael  MILLOT's avatar
Gael MILLOT committed
514
}
Gael  MILLOT's avatar
Gael MILLOT committed
515
516
517
518
519
520
521
522
523
524
# 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()
# end argument checking
# main code
if( ! any(class(data1) %in% c("matrix", "data.frame", "table"))){
return(head(data1, n))
}else{
obs.dim <- dim(data1)
row <- 1:ifelse(obs.dim[1] < n, obs.dim[1], n)
if(side == "l"){
col <- 1:ifelse(obs.dim[2] < n, obs.dim[2], n)
Gael  MILLOT's avatar
Gael MILLOT committed
525
}
Gael  MILLOT's avatar
Gael MILLOT committed
526
527
if(side == "r"){
col <- ifelse(obs.dim[2] < n, 1, obs.dim[2] - n + 1):obs.dim[2]
Gael  MILLOT's avatar
Gael MILLOT committed
528
}
Gael  MILLOT's avatar
Gael MILLOT committed
529
return(data1[row, col])
Gael  MILLOT's avatar
Gael MILLOT committed
530
531
532
533
}
}


Gael  MILLOT's avatar
Gael MILLOT committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
######## fun_tail() #### tail of the left or right of big 2D objects


# Check OK: clear to go Apollo
fun_tail <- function(data1, n = 10, side = "l"){
# AIM
# as tail() but display the left or right head of big 2D objects
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# ARGUMENTS
# data1: any object but more dedicated for matrix, data frame or table
# n: as in tail() but for for matrix, data frame or table, number of dimension to print (10 means 10 rows and columns)
# side: either "l" or "r" for the left or right side of the 2D object (only for matrix, data frame or table)
# BEWARE: other arguments of tail() not used
# RETURN
# the tail
# EXAMPLES
# obs1 = matrix(1:30, ncol = 5, dimnames = list(letters[1:6], LETTERS[1:5])) ; obs1 ; fun_tail(obs1, 3)
# obs1 = matrix(1:30, ncol = 5, dimnames = list(letters[1:6], LETTERS[1:5])) ; obs1 ; fun_tail(obs1, 3, "r")
# DEBUGGING
# data1 = matrix(1:10, ncol = 5) # for function debugging
# data1 = matrix(1:10, ncol = 5, dimnames = list(letters[1:2], LETTERS[1:5])) # for function debugging
# function name
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# end required function checking
# argument checking
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 = n, class = "vector", typeof = "integer", double.as.integer.allowed = TRUE, length = 1, fun.name = function.name) ; eval(ee)
tempo <- fun_check(data = side, options = c("l", "r"), length = 1, fun.name = function.name) ; eval(ee)
if(any(arg.check) == TRUE){
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) #
}
# 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()
# end argument checking
Gael  MILLOT's avatar
Gael MILLOT committed
577
# main code
Gael  MILLOT's avatar
Gael MILLOT committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
if( ! any(class(data1) %in% c("matrix", "data.frame", "table"))){
return(tail(data1, n))
}else{
obs.dim <- dim(data1)
row <- ifelse(obs.dim[1] < n, 1, obs.dim[1] - n + 1):obs.dim[1]
if(side == "l"){
col <- 1:ifelse(obs.dim[2] < n, obs.dim[2], n)
}
if(side == "r"){
col <- ifelse(obs.dim[2] < n, 1, obs.dim[2] - n + 1):obs.dim[2]
}
return(data1[row, col])
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
592
593


Gael  MILLOT's avatar
Gael MILLOT committed
594
######## fun_comp_1d() #### comparison of two 1D datasets (vectors, factors, 1D tables)
Gael  MILLOT's avatar
Gael MILLOT committed
595

Gael  MILLOT's avatar
Gael MILLOT committed
596
597
598
599

# Check OK: clear to go Apollo
fun_comp_1d <- function(data1, data2){
# AIM
600
# compare two 1D datasets (vector or factor or 1D table) of the same class or not. Check and report in a list if the 2 datasets have:
Gael  MILLOT's avatar
Gael MILLOT committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# same class
# common elements
# common element names (except factors)
# common levels (factors only)
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
# ARGUMENTS
# data1: vector or factor or 1D table
# data2: vector or factor or 1D table
# RETURN
# a list containing:
# $same.class: logical. Are class identical?
# $class: class of the 2 datasets (NULL otherwise)
# $same.length: logical. Are number of elements identical?
# $length: number of elements in the 2 datasets (NULL otherwise)
616
# $same.levels: logical. Are levels identical? NULL if data1 and data2 are not factors
Gael  MILLOT's avatar
Gael MILLOT committed
617
618
619
620
621
# $levels: levels of the 2 datasets if identical (NULL otherwise or NULL if data1 and data2 are not factors)
# $any.id.levels: logical. Is there any identical levels? (NULL if data1 and data2 are not factors)
# $same.levels.pos1: position, in data1, of the levels identical in data2 (NULL if data1 and data2 are not factors)
# $same.levels.pos2: position, in data2, of the levels identical in data1 (NULL if data1 and data2 are not factors)
# $common.levels: common levels between data1 and data2 (can be a subset of $levels or not). NULL if no common levels or if data1 and data2 are not factors
622
# $same.name: logical. Are element names identical? NULL if data1 and data2 have no names
Gael  MILLOT's avatar
Gael MILLOT committed
623
624
# $name: name of elements of the 2 datasets if identical (NULL otherwise)
# $any.id.name: logical. Is there any element names identical ?
625
626
# $same.name.pos1: position, in data1, of the element names identical in data2. NULL if no identical names
# $same.name.pos2: position, in data2, of the elements names identical in data1. NULL if no identical names
Gael  MILLOT's avatar
Gael MILLOT committed
627
628
# $common.names: common element names between data1 and data2 (can be a subset of $name or not). NULL if no common element names
# $any.id.element: logical. is there any identical elements ?
629
630
# $same.element.pos1: position, in data1, of the elements identical in data2. NULL if no identical elements
# $same.element.pos2: position, in data2, of the elements identical in data1. NULL if no identical elements
Gael  MILLOT's avatar
Gael MILLOT committed
631
# $common.elements: common elements between data1 and data2. NULL if no common elements
632
633
634
635
636
# $same.order: logical. Are all elements in the same order? TRUE or FALSE if elements of data1 and data2 are identical but not necessary in the same order. NULL otherwise (different length for instance)
# $order1: order of all elements of data1. NULL if $same.order is FALSE
# $order2: order of all elements of data2. NULL if $same.order is FALSE
# $identical.object: logical. Are objects identical (kind of object, element names, content, including content order)?
# $identical.content: logical. Are content objects identical (identical elements, including order, excluding kind of object and element names)?
Gael  MILLOT's avatar
Gael MILLOT committed
637
638
639
640
641
642
643
# EXAMPLES
# obs1 = 1:5 ; obs2 = 1:5 ; names(obs1) <- LETTERS[1:5] ; names(obs2) <- LETTERS[1:5] ; fun_comp_1d(obs1, obs2)
# obs1 = 1:5 ; obs2 = 1:5 ; names(obs1) <- LETTERS[1:5] ; fun_comp_1d(obs1, obs2)
# obs1 = 1:5 ; obs2 = 3:6 ; names(obs1) <- LETTERS[1:5] ; names(obs2) <- LETTERS[1:4] ; fun_comp_1d(obs1, obs2)
# obs1 = factor(LETTERS[1:5]) ; obs2 = factor(LETTERS[1:5]) ; fun_comp_1d(obs1, obs2)
# obs1 = factor(LETTERS[1:5]) ; obs2 = factor(LETTERS[10:11]) ; fun_comp_1d(obs1, obs2)
# obs1 = factor(LETTERS[1:5]) ; obs2 = factor(LETTERS[4:7]) ; fun_comp_1d(obs1, obs2)
644
# obs1 = factor(c(LETTERS[1:4], "E")) ; obs2 = factor(c(LETTERS[1:4], "F")) ; fun_comp_1d(obs1, obs2)
Gael  MILLOT's avatar
Gael MILLOT committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# obs1 = 1:5 ; obs2 = factor(LETTERS[1:5]) ; fun_comp_1d(obs1, obs2)
# obs1 = 1:5 ; obs2 = 1.1:6.1 ; fun_comp_1d(obs1, obs2)
# obs1 = as.table(1:5); obs2 = as.table(1:5) ; fun_comp_1d(obs1, obs2)
# obs1 = as.table(1:5); obs2 = 1:5 ; fun_comp_1d(obs1, obs2)
# DEBUGGING
# data1 = 1:5 ; data2 = 1:5 ; names(data1) <- LETTERS[1:5] ; names(data2) <- LETTERS[1:5] # for function debugging
# function name
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
# end function name
# argument checking
if( ! any(class(data1) %in% c("logical", "integer", "numeric", "character", "factor", "table"))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A NON NULL VECTOR, FACTOR OR 1D TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}else if(all(class(data1) %in% "table")){
if(length(dim(data1)) > 1){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A 1D TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
}
if( ! any(class(data2) %in% c("logical", "integer", "numeric", "character", "factor", "table"))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A NON NULL VECTOR, FACTOR OR 1D TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}else if(all(class(data2) %in% "table")){
if(length(dim(data2)) > 1){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A 1D TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
}
# 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
676
same.class <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
677
class <- NULL
678
same.length <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
679
length <- NULL
680
same.levels <- NULL # not FALSE to deal with no factors
Gael  MILLOT's avatar
Gael MILLOT committed
681
682
683
684
685
levels <- NULL
any.id.levels <- NULL
same.levels.pos1 <- NULL
same.levels.pos2 <- NULL
common.levels <- NULL
686
same.name <- NULL # not FALSE to deal with absence of name
Gael  MILLOT's avatar
Gael MILLOT committed
687
name <- NULL
688
any.id.name <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
689
690
691
same.name.pos1 <- NULL
same.name.pos2 <- NULL
common.names <- NULL
692
any.id.element <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
693
694
695
same.element.pos1 <- NULL
same.element.pos2 <- NULL
common.elements <- NULL
696
697
698
699
700
same.order <- NULL
order1 <- NULL
order2 <- NULL
identical.object <- FALSE
identical.content <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
if(identical(data1, data2)){
same.class <- TRUE
class <- class(data1)
same.length <- TRUE
length <- length(data1)
if(any(class(data1) %in% "factor")){
same.levels <- TRUE
levels <- levels(data1)
any.id.levels <- TRUE
same.levels.pos1 <- 1:length(levels(data1))
same.levels.pos2 <- 1:length(levels(data2))
common.levels <- levels(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)
common.names <- names(data1)
}
any.id.element <- TRUE
same.element.pos1 <- 1:length(data1)
same.element.pos2 <- 1:length(data2)
common.elements <- data1
726
727
728
same.order <- TRUE
order1 <- order(data1)
order2 <- order(data2)
Gael  MILLOT's avatar
Gael MILLOT committed
729
730
731
identical.object <- TRUE
identical.content <- TRUE
}else{
732
if(identical(class(data1), class(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
733
734
same.class <- TRUE
class <- class(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
735
}
736
if(identical(length(data1), length(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
737
738
same.length<- TRUE
length <- length(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
739
}
Gael  MILLOT's avatar
Gael MILLOT committed
740
if(any(class(data1) %in% "factor") & any(class(data2) %in% "factor")){
741
if(identical(levels(data1), levels(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
742
743
same.levels <- TRUE
levels <- levels(data1)
744
745
}else{
same.levels <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
746
}
Gael  MILLOT's avatar
Gael MILLOT committed
747
748
749
if(any(levels(data1) %in% levels(data2))){
any.id.levels <- TRUE
same.levels.pos1 <- which(levels(data1) %in% levels(data2))
Gael  MILLOT's avatar
Gael MILLOT committed
750
}
Gael  MILLOT's avatar
Gael MILLOT committed
751
752
753
if(any(levels(data2) %in% levels(data1))){
any.id.levels <- TRUE
same.levels.pos2 <- which(levels(data2) %in% levels(data1))
Gael  MILLOT's avatar
Gael MILLOT committed
754
}
Gael  MILLOT's avatar
Gael MILLOT committed
755
756
757
758
759
760
761
762
763
764
765
if(any.id.levels == TRUE){
common.levels <- unique(c(levels(data1)[same.levels.pos1], levels(data2)[same.levels.pos2]))
}
}
if(any(class(data1) %in% "factor")){ # to compare content
data1 <- as.character(data1)
}
if(any(class(data2) %in% "factor")){ # to compare content
data2 <- as.character(data2)
}
if( ! (is.null(names(data1)) & is.null(names(data2)))){
766
if(identical(names(data1), names(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
767
768
same.name <- TRUE
name <- names(data1)
769
770
}else{
same.name <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
771
772
773
774
775
776
777
778
779
780
781
782
}
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))
}
if(any.id.name == TRUE){
common.names <- unique(c(names(data1)[same.name.pos1], names(data2)[same.name.pos2]))
}
Gael  MILLOT's avatar
Gael MILLOT committed
783
}
784
785
names(data1) <- NULL # names solved -> to do not be disturbed by names
names(data2) <- NULL # names solved -> to do not be disturbed by names
Gael  MILLOT's avatar
Gael MILLOT committed
786
787
788
if(any(data1 %in% data2)){
any.id.element <- TRUE
same.element.pos1 <- which(data1 %in% data2)
Gael  MILLOT's avatar
Gael MILLOT committed
789
}
Gael  MILLOT's avatar
Gael MILLOT committed
790
791
792
if(any(data2 %in% data1)){
any.id.element <- TRUE
same.element.pos2 <- which(data2 %in% data1)
Gael  MILLOT's avatar
Gael MILLOT committed
793
}
Gael  MILLOT's avatar
Gael MILLOT committed
794
795
796
if(any.id.element == TRUE){
common.elements <- unique(c(data1[same.element.pos1], data2[same.element.pos2]))
}
797
if(identical(data1, data2)){
Gael  MILLOT's avatar
Gael MILLOT committed
798
identical.content <- TRUE
799
800
801
802
803
same.order <- TRUE
}else if(identical(sort(data1), sort(data2))){
same.order <- FALSE
order1 <- order(data1)
order2 <- order(data2)
Gael  MILLOT's avatar
Gael MILLOT committed
804
805
}
}
806
output <- list(same.class = same.class, class = class, same.length = same.length, length = length, same.levels = same.levels, levels = levels, any.id.levels = any.id.levels, same.levels.pos1 = same.levels.pos1, same.levels.pos2 = same.levels.pos2, common.levels = common.levels, same.name = same.name, name = name, any.id.name = any.id.name, same.name.pos1 = same.name.pos1, same.name.pos2 = same.name.pos2, common.names = common.names, any.id.element = any.id.element, same.element.pos1 = same.element.pos1, same.element.pos2 = same.element.pos2, common.elements = common.elements, same.order = same.order, order1 = order1, order2 = order2, identical.object = identical.object, identical.content = identical.content)
Gael  MILLOT's avatar
Gael MILLOT committed
807
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
808
809
810
}


Gael  MILLOT's avatar
Gael MILLOT committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
######## fun_comp_2d() #### comparison of two 2D datasets (row & col names, dimensions, etc.)


# Check OK: clear to go Apollo
fun_comp_2d <- function(data1, data2){
# AIM
# compare two 2D datasets of the same class or not. Check and report in a list if the 2 datasets have:
# same class
# common row names
# common column names
# same row number
# same column number
# potential identical rows between the 2 datasets
# potential identical columns between the 2 datasets
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
# ARGUMENTS
# data1: matrix, data frame or table
# data2: matrix, data frame or table
# RETURN
# a list containing:
# $same.class: logical. Are class identical ?
# $class: classes of the 2 datasets (NULL otherwise)
# $same.dim: logical. Are dimension identical ?
# $dim: dimension of the 2 datasets (NULL otherwise)
# $same.row.nb: logical. Are number of rows identical ?
# $row.nb: nb of rows of the 2 datasets if identical (NULL otherwise)
# $same.col.nb: logical. Are number of columns identical ?
# $col.nb: nb of columns of the 2 datasets if identical (NULL otherwise)
# $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
# $any.id.row: logical. is there identical rows (not considering row names) ?
# $same.row.pos1: position, in data1, of the rows identical in data2 (not considering row names)
# $same.row.pos2: position, in data2, of the rows identical in data1 (not considering row names)
# $any.id.col: logical. is there identical columns (not considering column names)?
# $same.col.pos1: position in data1 of the cols identical in data2 (not considering column names)
# $same.col.pos2: position in data2 of the cols identical in data1 (not considering column names)
# $identical.object: logical. Are objects identical (including row & column names)?
# $identical.content: logical. Are content objects identical (identical excluding row & column names)?
# EXAMPLES
# 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]))) ; obs1 ; obs2 ; fun_comp_2d(obs1, obs2)
# 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)
# 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
# data1 = data.frame(a = 1:3, b= letters[1:3], row.names = LETTERS[1:3]) ; data2 = data.frame(A = 1:3, B= letters[1:3]) # 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]))) # for function debugging
# 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
# 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]) # for function debugging
# function name
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
# end function name
# argument checking
if( ! any(class(data1) %in% c("matrix", "data.frame", "table"))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A MATRIX, DATA FRAME OR TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
if( ! any(class(data2) %in% c("matrix", "data.frame", "table"))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A MATRIX, DATA FRAME OR TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# 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
if(identical(data1, data2) & any(class(data1) %in% c("matrix", "data.frame", "table"))){
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
950
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
identical.object <- FALSE
if(all(class(data1) == "table") & length(dim(data1)) == 1){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 ARGUMENT IS A 1D TABLE. USE THE fun_comp_1d FUNCTION\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
if(all(class(data2) == "table") & length(dim(data2)) == 1){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data2 ARGUMENT IS A 1D TABLE. USE THE fun_comp_1d FUNCTION\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
if( ! identical(class(data1), class(data2))){
same.class <- FALSE
}else if( ! any(class(data1) %in% c("matrix", "data.frame", "table"))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 AND data2 ARGUMENTS MUST BE EITHER MATRIX, DATA FRAME OR TABLE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}else{
same.class <- TRUE
class <- class(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
968
}
Gael  MILLOT's avatar
Gael MILLOT committed
969
970
if( ! identical(dim(data1), dim(data2))){
same.dim <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
971
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
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
1000
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
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)
}else if(all(class(data1) == "matrix")){
data1 <- as.data.frame(data1, stringsAsFactors = FALSE)
}else if(all(class(data1) == "data.frame")){
data1 <- data.frame(lapply(data1, as.character), stringsAsFactors=FALSE)
}
if(all(class(data2) == "table")){
as.data.frame(matrix(data2, ncol = ncol(data2)), stringsAsFactors = FALSE)
}else if(all(class(data2) == "matrix")){
data2 <- as.data.frame(data2, stringsAsFactors = FALSE)
}else if(all(class(data2) == "data.frame")){
data2 <- data.frame(lapply(data2, as.character), stringsAsFactors=FALSE)
}
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
same.row.pos1 <- which(c(as.data.frame(t(data1), stringsAsFactors = FALSE)) %in% c(as.data.frame(t(data2), stringsAsFactors = FALSE)))
same.row.pos2 <- which(c(as.data.frame(t(data2), stringsAsFactors = FALSE)) %in% c(as.data.frame(t(data1), stringsAsFactors = FALSE)))
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
1065
}
Gael  MILLOT's avatar
Gael MILLOT committed
1066
1067
1068
1069
1070
1071
1072
1073
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
Gael  MILLOT's avatar
Gael MILLOT committed
1074
1075
}
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
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
same.col.pos1 <- which(c(data1) %in% c(data2))
same.col.pos2 <- which(c(data2) %in% c(data1))
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
1089
}
Gael  MILLOT's avatar
Gael MILLOT committed
1090
1091
if(all(is.na(same.col.pos2))){
same.col.pos2 <- NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1092
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1093
1094
1095
1096
1097
1098
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
}
Gael  MILLOT's avatar
Gael MILLOT committed
1099
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1100
1101
any.id.col <- FALSE
# same.col.pos1 and 2 remain NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1102
}
Gael  MILLOT's avatar
Gael MILLOT committed
1103
1104
1105
1106
1107
if(same.dim == TRUE & ! all(is.null(same.row.pos1), is.null(same.row.pos2), is.null(same.col.pos1), is.null(same.col.pos2))){ # same.dim == TRUE means that same.row.nb == TRUE and same.col.nb == TRUE, meaning that row.nb != NULL and col.nb != NULL. Thus, no need to include these checkings
if(identical(same.row.pos1, 1:row.nb) & identical(same.row.pos2, 1:row.nb) & identical(same.col.pos1, 1:col.nb) & identical(same.col.pos2, 1:col.nb)){
identical.content <- TRUE
}else{
identical.content <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
1108
1109
}
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
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
1170
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
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
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
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
# 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)?
# 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
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
# end function name
# argument checking
if( ! any(class(data1) %in% "list")){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A LIST\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
if( ! any(class(data2) %in% "list")){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A LIST\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# 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
1245
1246
1247
}


Gael  MILLOT's avatar
Gael MILLOT committed
1248
######## fun_test() #### test combinations of argument values of a function
Gael  MILLOT's avatar
Gael MILLOT committed
1249
1250


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

Gael  MILLOT's avatar
Gael MILLOT committed
1253
# Check OK: clear to go Apollo
Gael  MILLOT's avatar
tempo    
Gael MILLOT committed
1254
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
1255
1256
# AIM
# test combinations of argument values of a function
Gael  MILLOT's avatar
Gael MILLOT committed
1257
1258
# WARNING
# 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
1259
# ARGUMENTS
1260
1261
# 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
1262
# 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)
1263
# 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
1264
# thread.nb: numeric value indicating the number of available threads. Write NULL if no parallelization wanted
Gael  MILLOT's avatar
Gael MILLOT committed
1265
# 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
1266
# plot.fun: logical. Plot the plotting function tested for each test?
1267
1268
1269
# 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
# lib.path: character string indicating the absolute path of the required packages, if not in the default folders
Gael  MILLOT's avatar
Gael MILLOT committed
1270
# 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
1271
# REQUIRED PACKAGES
Gael  MILLOT's avatar
Gael MILLOT committed
1272
# lubridate
Gael  MILLOT's avatar
Gael MILLOT committed
1273
# parallel if thread.nb argument is not NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1274
# 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
1275
1276
1277
1278
1279
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# fun_get_message()
# fun_pack()
# RETURN
Gael  MILLOT's avatar
Gael MILLOT committed
1280
# if export is FALSE a list containing:
Gael  MILLOT's avatar
Gael MILLOT committed
1281
1282
1283
1284
1285
# $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
1286
# $expected.error: optional logical vector indicating the expected error specified in the expect.error argument
1287
1288
# $message: either NULL if $kind is always "OK", or the messages
# $instruction: the initial instruction
Gael  MILLOT's avatar
Gael MILLOT committed
1289
# $sys.info: system and packages info
1290
# 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
1291
1292
1293
1294
# one or several pdf if a plotting function is tested and if the plot.fun argument is TRUE
# 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)))
1295
# 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)
Gael  MILLOT's avatar
Gael MILLOT committed
1296
# 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-3.6.1\\library\\")
Gael  MILLOT's avatar
Gael MILLOT committed
1297
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10)) ; 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")))
Gael  MILLOT's avatar
Gael MILLOT committed
1298
1299
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10)) ; 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-3.6.1\\library\\")
# library(ggplot2) ; fun_test(fun = "geom_histogram", arg = c("data", "mapping"), val = list(x = list(data.frame(X = "a")), 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-3.6.1\\library\\") # BEWARE: ggplot2::geom_histogram does not work
Gael  MILLOT's avatar
Gael MILLOT committed
1300
# DEBUGGING
1301
1302
1303
1304
# 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
# set.seed(1) ; obs1 <- data.frame(Time = c(rnorm(10), rnorm(10) + 2), Group1 = rep(c("G", "H"), each = 10)) ; 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
1305
1306
# function name
function.name <- paste0(as.list(match.call(expand.dots=FALSE))[[1]], "()")
1307
instruction <- match.call()
Gael  MILLOT's avatar
Gael MILLOT committed
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# 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){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": REQUIRED ", i1, "() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
stop(tempo.cat)
}
}
# end required function checking
Gael  MILLOT's avatar
Gael MILLOT committed
1322
# argument primary checking
1323
1324
1325
1326
1327
1328
1329
# arg with no default values
if(any(missing(fun) | missing(arg) | missing(val))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": ARGUMENTS fun, arg AND val HAVE NO DEFAULT VALUE AND REQUIRE ONE\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# end arg with no default values
# using fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
1330
1331
1332
1333
1334
1335
1336
1337
1338
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
1339
1340
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
1341
1342
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
Gael  MILLOT's avatar
Gael MILLOT committed
1343
}else if( ! all(class(get(fun)) == "function")){
1344
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
1345
1346
1347
1348
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1349
tempo <- fun_check(data = arg, class = "vector", mode = "character", fun.name = function.name) ; eval(ee)
1350
1351
1352
1353
1354
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
1355
1356
tempo <- fun_check(data = val, class = "list", fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE){
1357
1358
1359
for(i2 in 1:length(val)){
tempo1 <- fun_check(data = val[[i2]], class = "vector", na.contain = TRUE, fun.name = function.name, print = FALSE)
tempo2 <- fun_check(data = val[[i2]], class = "list", na.contain = TRUE, fun.name = function.name, print = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1360
if(tempo1$problem == TRUE & tempo2$problem == TRUE){
1361
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
1362
1363
1364
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}else if(tempo1$problem == FALSE){ # vector split into list compartments
1365
val[[i2]] <- split(x = val[[i2]], f = 1:length(val[[i2]]))
Gael  MILLOT's avatar
Gael MILLOT committed
1366
1367
1368
}
}
}
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
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)){
tempo1 <- fun_check(data = expect.error[[i3]], class = "vector",  mode = "logical", fun.name = function.name, print = FALSE)
tempo2 <- fun_check(data =  expect.error[[i3]], class = "list", fun.name = function.name, print = FALSE)
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
1385
1386
1387
1388
1389
1390
1391
1392
if( ! is.null(thread.nb)){
tempo <- fun_check(data = thread.nb, typeof = "integer", double.as.integer.allowed = TRUE, neg.values = FALSE, length = 1, fun.name = "SLITHERINE") ; eval(ee)
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
1393
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
1394
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
1395
1396
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
1397
1398
1399
1400
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
1401
1402
1403
1404
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1405
}
Gael  MILLOT's avatar
Gael MILLOT committed
1406
if( ! is.null(lib.path)){
Gael  MILLOT's avatar
Gael MILLOT committed
1407
1408
1409
1410
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
1411
1412
1413
1414
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1415
}
Gael  MILLOT's avatar
Gael MILLOT committed
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
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
1426
1427
1428
if(any(arg.check) == TRUE){
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) #
}
1429
# end using fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
1430
# 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
1431
# end argument primary checking
Gael  MILLOT's avatar
Gael MILLOT committed
1432
# second round of checking and data preparation
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# 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))){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": NO ARGUMENT EXCEPT val CAN HAVE NA VALUES\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# 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)){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": THESE ARGUMENTS fun, arg, val, print.count, plot.fun AND export CANNOT BE NULL\n\n================\n\n")
stop(tempo.cat, call. = FALSE)
}
# end dealing with NULL
Gael  MILLOT's avatar
Gael MILLOT committed
1445
if(length(arg) != length(val)){
1446
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
1447
1448
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
}
Gael  MILLOT's avatar
Gael MILLOT committed
1449
1450
1451
args <- names(formals(get(fun)))
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 = " "))
1452
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1453
}
Gael  MILLOT's avatar
Gael MILLOT committed
1454
1455
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))
1456
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
1457
}
1458
1459
1460
1461
1462
1463
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
1464
1465
1466
1467
1468
1469
1470
1471
1472
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
1473
export <- TRUE
Gael  MILLOT's avatar
Gael MILLOT committed
1474
1475
1476
1477
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
1478
# package checking
Gael  MILLOT's avatar
Gael MILLOT committed
1479
fun_pack(req.package = c("lubridate"), lib.path = lib.path)
Gael  MILLOT's avatar
Gael MILLOT committed
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
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
1491
if(export == TRUE){
Gael  MILLOT's avatar
Gael MILLOT committed
1492
res.path <- paste0(res.path, "/fun_test_res_", trunc(ini.time))
Gael  MILLOT's avatar
Gael MILLOT committed
1493
1494
1495
1496
1497
1498
1499
if(dir.exists(res.path)){
tempo.cat <- paste0("\n\n================\n\nERROR IN ", function.name, ": FOLDER ALREADY EXISTS\n", res.path, "\nPLEASE RERUN ONCE\n\n============\n\n")
stop(tempo.cat, call. = FALSE)
}else{
dir.create(res.path)
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
1500
1501
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
1502
# creation of the txt instruction that includes several loops
Gael  MILLOT's avatar
Gael MILLOT committed
1503
1504
1505
1506
loop.string <- NULL
end.loop.string <- NULL
fun.args <- NULL
fun.args2 <- NULL
1507
error.values <- NULL