cute_little_R_functions.R 866 KB
Newer Older
Gael  MILLOT's avatar
Gael MILLOT committed
1
2
################################################################
##                                                            ##
Gael's avatar
Gael committed
3
##     CUTE FUNCTIONS                                         ##
Gael  MILLOT's avatar
Gael MILLOT committed
4
5
6
7
8
##                                                            ##
##     Gael A. Millot                                         ##
##                                                            ##
##                                                            ##
################################################################
Gael  MILLOT's avatar
Gael MILLOT committed
9
10


11
# color palette: see https://github.com/EmilHvitfeldt/r-color-palettes
Gael  MILLOT's avatar
Gael MILLOT committed
12
# https://usethis.r-lib.org/ and usethat also
13
# change everywhere: if( ! is.null(arg.check)){
Gael  MILLOT's avatar
Gael MILLOT committed
14
# BEWARE: do not forget to save the modifications in the .R file (through RSTUDIO for indentation)
Gael's avatar
tempo    
Gael committed
15
# update examples with good comment, as in fun_gg_boxplot
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
19
# check all(, na.rm = TRUE) and any(, na.rm = TRUE)
20
# fun_mat_fill does not recognize half matrix anymore
Gael  MILLOT's avatar
Gael MILLOT committed
21
22
23
24
25
# 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
26
27
28
# 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
29
# For heatmap: see https://bioinfo-fr.net/creer-des-heatmaps-a-partir-de-grosses-matrices-en-r
Gael  MILLOT's avatar
Gael MILLOT committed
30

Gael  MILLOT's avatar
Gael MILLOT committed
31

Gael  MILLOT's avatar
Gael MILLOT committed
32
################################ OUTLINE ################################
Gael  MILLOT's avatar
Gael MILLOT committed
33
34


Gael  MILLOT's avatar
Gael MILLOT committed
35
36
################ Object analysis    2
######## fun_check() #### check class, type, length, etc., of objects   2
Gael's avatar
Gael committed
37
38
39
40
41
######## fun_secu() #### verif that local variables are not present in other envs   11
######## fun_info() #### recover object information 13
######## fun_head() #### head of the left or right of big 2D objects    15
######## fun_tail() #### tail of the left or right of big 2D objects    16
######## fun_comp_1d() #### comparison of two 1D datasets (vectors, factors, 1D tables) 17
42
######## fun_comp_2d() #### comparison of two 2D datasets (row & col names, dimensions, etc.)   22
Gael's avatar
Gael committed
43
######## fun_comp_list() #### comparison of two lists   29
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
######## fun_test() #### test combinations of argument values of a function and return errors (and graphs)  32
################ Object modification    47
######## fun_name_change() #### check a vector of character strings and modify any string if present in another vector  47
######## fun_df_remod() #### remodeling a data frame to have column name as a qualitative values and vice-versa 48
######## fun_round() #### rounding number if decimal present    51
######## fun_mat_rotate() #### 90° clockwise matrix rotation    53
######## fun_mat_num2color() #### convert a numeric matrix into hexadecimal color matrix    54
######## fun_mat_op() #### assemble several matrices with operation 58
######## fun_mat_inv() #### return the inverse of a square matrix   60
######## fun_mat_fill() #### fill the empty half part of a symmetric square matrix  62
######## fun_permut() #### progressively breaks a vector order  65
######## fun_slide() #### return a computation made on a vector using a sliding window  76
################ Graphics management    85
######## fun_width() #### window width depending on classes to plot 85
######## fun_open() #### open a GUI or pdf graphic window   87
######## fun_prior_plot() #### set graph param before plotting (erase axes for instance)    91
######## fun_scale() #### select nice label numbers when setting number of ticks on an axis 95
######## fun_inter_ticks() #### define coordinates of secondary ticks   100
######## fun_post_plot() #### set graph param after plotting (axes redesign for instance)   105
######## fun_close() #### close specific graphic windows    117
################ Standard graphics  118
######## fun_empty_graph() #### text to display for empty graphs    118
################ gg graphics    120
######## fun_gg_palette() #### ggplot2 default color palette    120
######## fun_gg_just() #### ggplot2 justification of the axis labeling, depending on angle  122
######## fun_gg_get_legend() #### get the legend of ggplot objects  127
######## fun_gg_point_rast() #### ggplot2 raster scatterplot layer  129
######## fun_gg_boxplot() #### ggplot2 boxplot + background dots if required    133
######## fun_gg_scatter() #### ggplot2 scatterplot + lines (up to 6 overlays totally)   133
######## fun_gg_heatmap() #### ggplot2 heatmap + overlaid mask if required  133
######## fun_gg_empty_graph() #### text to display for empty graphs 141
################ Graphic extraction 143
######## fun_trim() #### display values from a quantitative variable and trim according to defined cut-offs 143
######## fun_segmentation() #### segment a dot cloud on a scatterplot and define the dots from another cloud outside the segmentation   152
################ Import 187
######## fun_pack() #### check if R packages are present and import into the working environment    187
######## fun_python_pack() #### check if python packages are present    189
################ Print / Exporting results (text & tables)  192
######## fun_report() #### print string or data object into output file 192
######## fun_get_message() #### return error/warning/other messages of an expression (that can be exported) 195
Gael  MILLOT's avatar
Gael MILLOT committed
84
85


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

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

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


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


95
# Check r_debugging_tools-v1.2.R OK
96
97
# Check fun_test() (see cute_checks.docx) Ok
# check manual: example to scan again
98
# clear to go Apollo
Gael's avatar
tempo    
Gael committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
){
Gael  MILLOT's avatar
Gael MILLOT committed
115
116
117
118
# 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?
119
# 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
120
# if options is non null, then class, type and mode must be NULL, and length can be NULL or specified
121
# WARNINGS
Gael's avatar
Gael committed
122
123
# The function tests what is written in arguments, even if what is written in incoherent. For instance, fun_check(data = factor(1), class = "factor", mode = "character") will return a problem, and this, what ever the object tested in the data argument, because no object can be class "factor" and mode "character" (factors are class "factor" and mode "numeric")
# Since R >= 4.0.0, class(matrix()) returns "matrix" "array", and not "matrix" alone as before. However, use argument class = "matrix" to check for matrix object (of class "matrix" "array" in R >= 4.0.0) and use argument class = "array" to check for array object (of class "array" in R >= 4.0.0)
Gael  MILLOT's avatar
Gael MILLOT committed
124
125
# ARGUMENTS
# data: object to test
126
# 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
127
# class: character string. Either one of the class() result (But see the warning section above) or "vector" or "ggplot2" (i.e., objects of class c("gg", "ggplot")) or NULL
128
# typeof: character string. Either one of the typeof() result or NULL
129
# mode: character string. Either one of the mode() result (for non vector object) or NULL
130
131
# 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.
132
# 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 strictly have 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. WARNING: data%%1 == 0 but not isTRUE(all.equal(data%%1, 0)) is used here because the argument checks for integers stored as double (does not check for decimal numbers that are approximate integers)
133
# options: a vector of character strings indicating all the possible option values for data
134
# 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
135
# na.contain: logical. Can data contain NA?
136
# 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
Gael  MILLOT's avatar
Gael MILLOT committed
137
# print: logical. Print the error message if $problem is TRUE? WARNING: set by default to FALSE, which facilitates the control of the error message output when using fun_check() inside functions. See the example section
138
# 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
139
140
# RETURN
# a list containing:
141
# $problem: logical. Is there any problem detected?
Gael  MILLOT's avatar
Gael MILLOT committed
142
143
# $text: the problem detected
# $fun.name: name of the checked parameter
Gael's avatar
tempo    
Gael committed
144
145
146
147
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
Gael  MILLOT's avatar
Gael MILLOT committed
148
149
150
151
152
153
154
# 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
Gael  MILLOT's avatar
Gael MILLOT committed
155
# if(tempo$problem == TRUE){cat(paste0("\n\n================\n\n", tempo$text, "\n\n================\n\n"))}
Gael  MILLOT's avatar
Gael MILLOT committed
156
157
158
159
160
161
# 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
162
163
# fun.name checked first because required next
if( ! is.null(fun.name)){
Gael's avatar
Gael committed
164
if( ! (all(class(fun.name) == "character") & length(fun.name) == 1)){
165
166
tempo.cat <- paste0("ERROR IN fun_check(): THE fun.name ARGUMENT MUST BE A CHARACTER VECTOR OF LENGTH 1: ", paste(fun.name, collapse = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
167
168
169
}
}
# end fun.name checked first because required next
170
171
# arg with no default values
if(missing(data)){
172
173
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": ARGUMENT data HAS NO DEFAULT VALUE AND REQUIRES ONE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
174
175
}
# end arg with no default values
176
177
# 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))){
178
179
tempo.cat <- paste0("ERROR 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 = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
180
181
182
}
# end dealing with NA
# dealing with NULL
183
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)){
184
185
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THESE ARGUMENTS\nprop\ndouble.as.integer.allowed\nall.options.in.data\nna.contain\nneg.values\nprint\nCANNOT 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 = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
186
187
188
189
190
}
# end dealing with NULL
# dealing with logical
# tested below
# end dealing with logical
Gael  MILLOT's avatar
Gael MILLOT committed
191
if( ! is.null(data.name)){
Gael's avatar
Gael committed
192
if( ! (length(data.name) == 1 & all(class(data.name) == "character"))){
193
194
tempo.cat <- paste0("ERROR 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 = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
195
}
Gael  MILLOT's avatar
Gael MILLOT committed
196
}
Gael  MILLOT's avatar
Gael MILLOT committed
197
if(is.null(options) & is.null(class) & is.null(typeof) & is.null(mode) &  prop == FALSE & is.null(length)){
198
199
tempo.cat <- paste0("ERROR 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)")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
200
}
Gael  MILLOT's avatar
Gael MILLOT committed
201
if( ! is.null(options) & ( ! is.null(class) | ! is.null(typeof) | ! is.null(mode) | prop == TRUE)){
202
203
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
204
}
Gael  MILLOT's avatar
Gael MILLOT committed
205
if( ! (all(class(neg.values) == "logical") & length(neg.values) == 1 & any(is.na(neg.values)) != TRUE)){
206
207
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": THE neg.values ARGUMENT MUST BE TRUE OR FALSE ONLY")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
208
}
Gael  MILLOT's avatar
Gael MILLOT committed
209
if(neg.values == FALSE & is.null(class) & is.null(typeof) & is.null(mode)){
210
211
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
212
}
Gael  MILLOT's avatar
Gael MILLOT committed
213
if( ! is.null(class)){
Gael's avatar
Gael committed
214
if( ! all(class %in% c("vector", "logical", "integer", "numeric", "complex", "character", "matrix", "array", "data.frame", "list", "factor", "table", "expression", "name", "symbol", "function", "uneval", "environment", "ggplot2", "ggplot_built") & any(is.na(class)) != TRUE & length(class) == 1)){ # length == 1 here because of class(matrix()) since R4.0.0
215
216
tempo.cat <- paste0("ERROR 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\", \"ggplot2\", \"ggplot_built\"")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
217
}
Gael  MILLOT's avatar
Gael MILLOT committed
218
if(neg.values == FALSE & ! any(class %in% c("vector", "numeric", "integer", "matrix", "array", "data.frame", "table"))){
219
220
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
221
222
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
223
if( ! is.null(typeof)){
224
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)){
225
226
tempo.cat <- paste0("ERROR 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\"")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
227
}
Gael  MILLOT's avatar
Gael MILLOT committed
228
if(neg.values == FALSE & ! typeof %in% c("double", "integer")){
229
230
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
231
232
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
233
if( ! is.null(mode)){
234
if( ! (all(mode %in% c("logical", "numeric", "complex", "character", "list", "expression", "name", "symbol", "function", "environment", "S4")) & length(mode) == 1 & any(is.na(mode)) != TRUE)){
235
236
tempo.cat <- paste0("ERROR 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\"")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
237
}
Gael  MILLOT's avatar
Gael MILLOT committed
238
if(neg.values == FALSE & mode != "numeric"){
239
240
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
241
242
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
243
244
if( ! is.null(length)){
if( ! (is.numeric(length) & length(length) == 1 & ! grepl(length, pattern = "\\.") & any(is.na(length)) != TRUE)){
245
246
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": length ARGUMENT MUST BE A SINGLE INTEGER VALUE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
247
248
}
}
249
if( ! (is.logical(prop) | (length(prop) == 1 & any(is.na(prop)) != TRUE))){
250
251
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" IN ", fun.name)), ": prop ARGUMENT MUST BE TRUE OR FALSE ONLY")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
252
253
}else if(prop == TRUE){
if( ! is.null(class)){
254
if( ! any(class %in% c("vector", "numeric", "matrix", "array", "data.frame", "table"))){
255
256
tempo.cat <- paste0("ERROR 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") # not integer because prop
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
257
258
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
259
260
if( ! is.null(mode)){
if(mode != "numeric"){
261
262
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
263
264
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
265
266
if( ! is.null(typeof)){
if(typeof != "double"){
267
268
tempo.cat <- paste0("ERROR 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")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
269
270
271
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
272
if( ! (all(class(double.as.integer.allowed) == "logical") & length(double.as.integer.allowed) == 1 & any(is.na(double.as.integer.allowed)) != TRUE)){
273
274
tempo.cat <- paste0("ERROR 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 = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
275
}
Gael  MILLOT's avatar
Gael MILLOT committed
276
if( ! (is.logical(all.options.in.data) & length(all.options.in.data) == 1 & any(is.na(all.options.in.data)) != TRUE)){
277
278
tempo.cat <- paste0("ERROR 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 = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
279
}
Gael  MILLOT's avatar
Gael MILLOT committed
280
if( ! (all(class(na.contain) == "logical") & length(na.contain) == 1 & any(is.na(na.contain)) != TRUE)){
281
282
tempo.cat <- paste0("ERROR IN fun_check(): THE na.contain ARGUMENT MUST BE TRUE OR FALSE ONLY: ", paste(na.contain, collapse = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
283
}
Gael  MILLOT's avatar
Gael MILLOT committed
284
if( ! (all(class(print) == "logical") & length(print) == 1 & any(is.na(print)) != TRUE)){
285
286
tempo.cat <- paste0("ERROR IN fun_check(): THE print ARGUMENT MUST BE TRUE OR FALSE ONLY: ", paste(print, collapse = " "))
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
287
}
288
# fun.name tested at the beginning
Gael  MILLOT's avatar
Gael MILLOT committed
289
290
291
292
293
294
295
296
297
298
# 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 <- ""
299
if( ! all(data %in% options, na.rm = TRUE)){
Gael  MILLOT's avatar
Gael MILLOT committed
300
problem <- TRUE
301
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
302
303
}
if(all.options.in.data == TRUE){
304
if( ! all(options %in% data)){ # no need of na.rm = TRUE for all because %in% does not output NA
Gael  MILLOT's avatar
Gael MILLOT committed
305
problem <- TRUE
306
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
307
}
Gael  MILLOT's avatar
Gael MILLOT committed
308
}
Gael  MILLOT's avatar
Gael MILLOT committed
309
310
311
if( ! is.null(length)){
if(length(data) != length){
problem <- TRUE
312
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
313
314
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
315
316
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
317
}
Gael  MILLOT's avatar
Gael MILLOT committed
318
}
Gael  MILLOT's avatar
Gael MILLOT committed
319
arg.names <- c("class", "typeof", "mode", "length")
Gael's avatar
Gael committed
320
321
322
323
324
325
326
if( ! is.null(class)){
if(class == "matrix"){ # because of class(matric()) since R4.0.0
class <- c("matrix", "array")
}else if(class == "factor" & all(class(data) %in% c("factor", "ordered"))){ # to deal with ordered factors
class <- c("factor", "ordered")
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
327
328
if(is.null(options)){
for(i2 in 1:length(arg.names)){
329
if( ! is.null(get(arg.names[i2], env = sys.nframe(), inherit = FALSE))){
Gael  MILLOT's avatar
Gael MILLOT committed
330
331
332
333
334
# 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
335
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
336
text <- paste0(text, " AND ") ; 
Gael  MILLOT's avatar
Gael MILLOT committed
337
}
338
text <- paste0(text, toupper(arg.names[i2]), " ", if(all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) %in% c("matrix", "array"))){"matrix"}else if(all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) %in% c("factor", "ordered"))){"factor"}else{get(arg.names[i2], env = sys.nframe(), inherit = FALSE)})
Gael  MILLOT's avatar
Gael MILLOT committed
339
340
'
# end script to execute
341
if(typeof(data) == "double" & double.as.integer.allowed == TRUE & ((arg.names[i2] == "class" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "integer")) | (arg.names[i2] == "typeof" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "integer")))){
342
if( ! all(data %% 1 == 0, na.rm = TRUE)){ # 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
Gael  MILLOT's avatar
Gael MILLOT committed
343
eval(parse(text = tempo.script)) # execute tempo.script
Gael  MILLOT's avatar
Gael MILLOT committed
344
}
345
}else if( ! any(all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) %in% c("vector", "ggplot2"))) & ! all(eval(parse(text = paste0(arg.names[i2], "(data)"))) %in% get(arg.names[i2], env = sys.nframe(), inherit = FALSE))){ # test the four c("class", "typeof", "mode", "length") arguments with their corresponding function. No need of na.rm = TRUE for all because %in% does not output NA
Gael  MILLOT's avatar
Gael MILLOT committed
346
eval(parse(text = tempo.script)) # execute tempo.script
347
}else if(arg.names[i2] == "class" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "vector") & ! (all(class(data) %in% "numeric") | all(class(data) %in% "integer") | all(class(data) %in% "character") | all(class(data) %in% "logical"))){ # test class == "vector". No need of na.rm = TRUE for all because %in% does not output NA
348
eval(parse(text = tempo.script)) # execute tempo.script
349
}else if(arg.names[i2] == "class" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "ggplot2") & ! all(class(data) %in% c("gg", "ggplot"))){ # test ggplot object
Gael  MILLOT's avatar
Gael MILLOT committed
350
eval(parse(text = tempo.script)) # execute tempo.script
Gael  MILLOT's avatar
Gael MILLOT committed
351
352
353
354
}
}
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
355
if(prop == TRUE){
Gael  MILLOT's avatar
Gael MILLOT committed
356
if(is.null(data) | any(data < 0 | data > 1, na.rm = TRUE)){
Gael  MILLOT's avatar
Gael MILLOT committed
357
358
359
360
361
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
362
}
Gael  MILLOT's avatar
Gael MILLOT committed
363
text <- paste0(text, "THE ", data.name, " PARAMETER MUST BE DECIMAL VALUES BETWEEN 0 AND 1")
Gael  MILLOT's avatar
Gael MILLOT committed
364
}
365
}
366
if(all(class(data) %in% "expression")){  # no need of na.rm = TRUE for all because %in% does not output NA
Gael  MILLOT's avatar
Gael MILLOT committed
367
data <- as.character(data) # to evaluate the presence of NA
Gael  MILLOT's avatar
Gael MILLOT committed
368
}
369
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
370
371
372
373
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
374
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
375
text <- paste0(text, " AND ")
Gael  MILLOT's avatar
Gael MILLOT committed
376
}
Gael  MILLOT's avatar
Gael MILLOT committed
377
text <- paste0(text, "THE ", data.name, " PARAMETER CONTAINS NA WHILE NOT AUTHORIZED")
Gael  MILLOT's avatar
Gael MILLOT committed
378
}
Gael  MILLOT's avatar
Gael MILLOT committed
379
}
Gael  MILLOT's avatar
Gael MILLOT committed
380
381
382
383
384
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
385
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
386
text <- paste0(text, " AND ")
Gael  MILLOT's avatar
Gael MILLOT committed
387
}
Gael  MILLOT's avatar
Gael MILLOT committed
388
text <- paste0(text, "THE ", data.name, " PARAMETER MUST BE NON NEGATIVE NUMERIC VALUES")
Gael  MILLOT's avatar
Gael MILLOT committed
389
390
}
}
Gael  MILLOT's avatar
Gael MILLOT committed
391
392
if(print == TRUE & problem == TRUE){
cat(paste0("\n\n================\n\n", text, "\n\n================\n\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
393
}
Gael  MILLOT's avatar
Gael MILLOT committed
394
395
output <- list(problem = problem, text = text, fun.name = data.name)
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
396
}
Gael  MILLOT's avatar
Gael MILLOT committed
397

398
######## fun_secu() #### verif that local variables are not present in other envs
Gael  MILLOT's avatar
Gael MILLOT committed
399
400
401
402
403
404


fun_secu <- function(pos = 1, name = NULL){
# AIM
# verif that local variables are not present in other environments, in order to avoid scope preference usage. The fun_secu() function checks by default the parent environment. This means that when used inside a function, it checks the local environment of this function. When used in the Global environment, it would check this environment
# ARGUMENTS
405
# pos: single integer indicating the position of the environment checked (argument n of parent.frame()). VaLue 1 means one step above the fun_secu() local environment. Thus, if fun_secu() is used in the working environment, with pos ==1, variables of this env will be checked in the above envs. If fun_secu() is used in a function, with pos ==1, variables presents in the local env of the functions will be checked in the above envs (which includes the working environment (Global env)
Gael  MILLOT's avatar
Gael MILLOT committed
406
407
408
# name: single character string indicating the name of the function checked
# RETURN
# a character string of the local variables that match variables in the different environments of the R scope, or NULL if no match
Gael's avatar
tempo    
Gael committed
409
410
411
412
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
413
414
415
416
417
418
419
420
# EXAMPLES
# fun_secu()
# fun_secu(pos = 2)
# mean <- 0 ; fun1 <- function(){sd <- 1 ; fun_secu(name = as.character(sys.calls()[[length(sys.calls())]]))} ; fun2 <- function(){cor <- 2 ; fun1()} ; fun1() ; fun2() ; rm(mean) # sys.calls() gives the the function name at top stack of the imbricated functions, sys.calls()[[length(sys.calls())]] the name of the just above function. This can also been used for the above function: as.character(sys.call(1))
# test.pos <- 2 ; mean <- 0 ; fun1 <- function(){sd <- 1 ; fun_secu(pos = test.pos, name = if(length(sys.calls()) >= test.pos){as.character(sys.calls()[[length(sys.calls()) + 1 - test.pos]])}else{search()[ (1:length(search()))[test.pos - length(sys.calls())]]})} ; fun2 <- function(){cor <- 2 ; fun1()} ; fun1() ; fun2() ; rm(mean) # for argument name, here is a way to have the name of the tested environment according to test.pos value
# DEBUGGING
# pos = 1 ; name = NULL # for function debugging
# function name
421
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
422
423
424
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
425
426
tempo.cat <- paste0("ERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
}
# 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 = pos, class = "vector", typeof = "integer", double.as.integer.allowed = TRUE, length = 1, fun.name = function.name) ; eval(ee)
if( ! is.null(name)){
tempo <- fun_check(data = name, class = "vector", typeof = "character", 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
# main code
444
# match.list <- vector("list", length = (length(sys.calls()) - 1 + length(search()) + ifelse(length(sys.calls()) == 1, -1, 0))) # match.list is a list of all the environment tested (local of functions and R envs), length(sys.calls()) - 1 to remove the level of the fun_secu() function, sys.calls() giving all the names of the imbricated functions, including fun_secu, ifelse(length(sys.calls()) == 1, -1, 0) to remove Global env if this one is tested
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
tempo.name <- rev(as.character(unlist(sys.calls()))) # get names of frames (i.e., enclosed env)
tempo.frame <- rev(sys.frames())  # get frames (i.e., enclosed env)
# dealing with source()
# source() used in the Global env creates three frames above the Global env, which should be removed because not very interesting for variable duplications. Add a <<-(sys.frames()) in this code and source anova_contrasts code to see this. With ls(a[[4]]), we can see the content of each env, which are probably elements of source()
if(any(sapply(tempo.frame, FUN = environmentName) %in% "R_GlobalEnv")){
global.pos <- which(sapply(tempo.frame, FUN = environmentName) %in% "R_GlobalEnv")
# remove the global env (because already in search(), and all the oabove env
tempo.name <- tempo.name[-c(global.pos:length(tempo.frame))]
tempo.frame <- tempo.frame[-c(global.pos:length(tempo.frame))]
}
# end dealing with source()
# might have a problem if(length(tempo.name) == 0){
match.list <- vector("list", length = length(tempo.name) + length(search())) # match.list is a list of all the environment tested (local of functions and R envs), length(sys.calls()) - 1 to remove the level of the fun_secu() function, sys.calls() giving all the names of the imbricated functions, including fun_secu, ifelse(length(sys.calls()) == 1, -1, 0) to remove Global env if this one is tested
ls.names <- c(tempo.name, search()) # names of the functions + names of the search() environments
ls.input <- c(tempo.frame, as.list(search())) # environements of the functions + names of the search() environments
names(match.list) <- ls.names # 
match.list <- match.list[-c(1:(pos + 1))] # because we check only above pos
462
463
464
465
466
ls.tested <- ls.input[[pos + 1]]
ls.input <- ls.input[-c(1:(pos + 1))]
for(i1 in 1:length(match.list)){
if(any(ls(name = ls.input[[i1]], all.names = TRUE) %in% ls(name = ls.tested, all.names = TRUE))){
match.list[i1] <- list(ls(name = ls.input[[i1]], all.names = TRUE)[ls(name = ls.input[[i1]], all.names = TRUE) %in% ls(name = ls.tested, all.names = TRUE)])
Gael  MILLOT's avatar
Gael MILLOT committed
467
468
469
}
}
if( ! all(sapply(match.list, FUN = is.null))){
470
output <- paste0("SOME VARIABLES ", ifelse(is.null(name), "OF THE CHECKED ENVIRONMENT", paste0("OF ", name)), " ARE ALSO PRESENT IN :\n", paste0(names(match.list[ ! sapply(match.list, FUN = is.null)]), ": ", sapply(match.list[ ! sapply(match.list, FUN = is.null)], FUN = paste0, collapse = " "), collapse = "\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
471
472
473
474
475
476
}else{
output <- NULL
}
return(output)
}

Gael  MILLOT's avatar
Gael MILLOT committed
477
478
479
480
481

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


# Check OK: clear to go Apollo
482
fun_info <- function(data, n = 20, full = FALSE, warn.print = TRUE){
Gael  MILLOT's avatar
Gael MILLOT committed
483
# AIM
484
# provide a complete description of an object. Using complete = TRUE allows data recovering. Using complete = FALSE is a convenient display feature (in case of large dataset)
Gael  MILLOT's avatar
Gael MILLOT committed
485
486
# ARGUMENTS
# data: object to test
487
488
489
# n: positive integer value indicating the number of element to display per compartment of the output list (i.e., head(..., n)). Ignored if full argument is TRUE. Also ignored for the STRUCTURE compartment output, corresponding to ls.str() information (because head() removes almost everything)
# full: logical. Return the full information?
# warn.print: logical. Print warnings at the end of the execution? No print if no warning messages
Gael  MILLOT's avatar
Gael MILLOT committed
490
491
492
# 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
Gael's avatar
tempo    
Gael committed
493
494
495
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
496
# fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
497
498
# EXAMPLES
# fun_info(data = 1:3)
499
500
# fun_info(data = 1:3, n = 2)
# fun_info(data = 1:3, n = 2, full = TRUE)
501
# fun_info(data.frame(a = 1:2, b = ordered(factor(c("A", "B"))), stringsAsFactors = TRUE))
Gael  MILLOT's avatar
Gael MILLOT committed
502
503
504
505
506
# 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
507
# data = data.frame(a = 1:2, b = c("A", "B"), stringsAsFactors = TRUE) # for function debugging
Gael  MILLOT's avatar
Gael MILLOT committed
508
509
510
511
# 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
512
513
514
515
516
517
518
519
520
# 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("ERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}
# end required function checking
Gael  MILLOT's avatar
Gael MILLOT committed
521
# argument checking
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
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", length = 1, double.as.integer.allowed = TRUE, fun.name = function.name) ; eval(ee)
if(tempo$problem == FALSE & n < 1){
tempo.cat <- paste0("ERROR IN ", function.name, ": n ARGUMENT MUST BE A POSITIVE AND NON NULL INTEGER")
text.check <- c(text.check, tempo.cat)
arg.check <- c(arg.check, TRUE)
}
tempo <- fun_check(data = full, class = "logical", length = 1, fun.name = function.name) ; eval(ee)
tempo <- fun_check(data = warn.print, class = "logical", 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
537
# end argument checking
538
# 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
Gael  MILLOT's avatar
Gael MILLOT committed
539
# main code
540
541
542
543
544
on.exit(
exp = if(full == FALSE & warn.print == TRUE){
warning(paste0("FROM ", function.name, ":\n\nSOME COMPARTMENTS CAN BE TRUNCATED (n ARGUMENT IS ", n, ")\n\n"), call. = FALSE)
}
)
Gael  MILLOT's avatar
Gael MILLOT committed
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
577
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)
}
578
if(all(class(data) == "data.frame" | all(class(data) %in% c("matrix", "array")))){
Gael  MILLOT's avatar
Gael MILLOT committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
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
594
}
Gael  MILLOT's avatar
Gael MILLOT committed
595
tempo[["COLUMN_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
Gael  MILLOT's avatar
Gael MILLOT committed
596
}
Gael  MILLOT's avatar
Gael MILLOT committed
597
output <- c(output, tempo)
Gael  MILLOT's avatar
Gael MILLOT committed
598
}
Gael  MILLOT's avatar
Gael MILLOT committed
599
600
601
602
603
604
605
606
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
607
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
608
tempo2 <- unlist(tempo.class)
Gael  MILLOT's avatar
Gael MILLOT committed
609
}
Gael  MILLOT's avatar
Gael MILLOT committed
610
tempo[["COMPARTMENT_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
Gael  MILLOT's avatar
Gael MILLOT committed
611
}
Gael  MILLOT's avatar
Gael MILLOT committed
612
output <- c(output, tempo)
613
614
615
616
617
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)
}
if(full == FALSE){
output[names(output) != "STRUCTURE"] <- lapply(X = output[names(output) != "STRUCTURE"], FUN = head, n = n, simplify = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
618
}
Gael  MILLOT's avatar
Gael MILLOT committed
619
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
620
}
Gael  MILLOT's avatar
Gael MILLOT committed
621
622
623
624
625
626


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


# Check OK: clear to go Apollo
Gael's avatar
tempo    
Gael committed
627
628
629
630
631
fun_head <- function(
data1, 
n = 6, 
side = "l"
){
Gael  MILLOT's avatar
Gael MILLOT committed
632
633
634
635
636
637
638
639
640
# AIM
# as head() but display the left or right head of big 2D objects
# 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
Gael's avatar
tempo    
Gael committed
641
642
643
644
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
645
646
647
648
649
650
651
# 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
652
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
653
654
655
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
656
657
tempo.cat <- paste0("ERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
658
}
Gael  MILLOT's avatar
Gael MILLOT committed
659
660
661
662
663
664
665
666
667
668
# 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
669
}
Gael  MILLOT's avatar
Gael MILLOT committed
670
671
672
# 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
673
if( ! (any(class(data1) %in% c("data.frame", "table")) | all(class(data1) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data1) %in% c("matrix", "data.frame", "table"))
Gael  MILLOT's avatar
Gael MILLOT committed
674
675
676
677
678
679
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
680
}
Gael  MILLOT's avatar
Gael MILLOT committed
681
682
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
683
}
Gael  MILLOT's avatar
Gael MILLOT committed
684
return(data1[row, col])
Gael  MILLOT's avatar
Gael MILLOT committed
685
686
687
688
}
}


Gael  MILLOT's avatar
Gael MILLOT committed
689
690
691
692
######## fun_tail() #### tail of the left or right of big 2D objects


# Check OK: clear to go Apollo
Gael's avatar
tempo    
Gael committed
693
694
695
696
697
fun_tail <- function(
data1, 
n = 10, 
side = "l"
){
Gael  MILLOT's avatar
Gael MILLOT committed
698
699
700
701
702
703
704
705
706
# AIM
# as tail() but display the left or right head of big 2D objects
# 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
Gael's avatar
tempo    
Gael committed
707
708
709
710
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
Gael  MILLOT's avatar
Gael MILLOT committed
711
712
713
714
715
716
717
# 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
718
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
719
720
721
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
722
723
tempo.cat <- paste0("ERROR IN ", function.name, ": REQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
}
# 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
738
# main code
739
if( ! (any(class(data1) %in% c("data.frame", "table")) | all(class(data1) %in% c("matrix", "array")))){ # before R4.0.0, it was  ! any(class(data1) %in% c("matrix", "data.frame", "table"))
Gael  MILLOT's avatar
Gael MILLOT committed
740
741
742
743
744
745
746
747
748
749
750
751
752
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
753
754


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

Gael  MILLOT's avatar
Gael MILLOT committed
757
758
759
760

# Check OK: clear to go Apollo
fun_comp_1d <- function(data1, data2){
# AIM
761
# compare two 1D datasets (vector or factor or 1D table, or 1D matrix or 1D array) of the same class or not. Check and report in a list if the 2 datasets have:
Gael  MILLOT's avatar
Gael MILLOT committed
762
763
764
765
766
# same class
# common elements
# common element names (except factors)
# common levels (factors only)
# ARGUMENTS
767
768
# data1: vector or factor or 1D table, or 1D matrix or 1D array
# data2: vector or factor or 1D table, or 1D matrix or 1D array
Gael  MILLOT's avatar
Gael MILLOT committed
769
770
771
772
773
774
# 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)
775
# $same.levels: logical. Are levels identical? NULL if data1 and data2 are not factors
Gael  MILLOT's avatar
Gael MILLOT committed
776
777
778
779
780
# $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
781
# $same.name: logical. Are element names identical? NULL if data1 and data2 have no names
Gael  MILLOT's avatar
Gael MILLOT committed
782
783
# $name: name of elements of the 2 datasets if identical (NULL otherwise)
# $any.id.name: logical. Is there any element names identical ?
784
785
# $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
786
787
# $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 ?
788
789
# $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
790
# $common.elements: common elements between data1 and data2. NULL if no common elements
791
792
793
794
795
# $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's avatar
tempo    
Gael committed
796
797
798
799
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# none
Gael  MILLOT's avatar
Gael MILLOT committed
800
801
802
803
804
805
806
# 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)
807
# 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
808
809
810
811
812
813
814
# 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
815
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
Gael  MILLOT's avatar
Gael MILLOT committed
816
817
818
# end function name
# argument checking
if( ! any(class(data1) %in% c("logical", "integer", "numeric", "character", "factor", "table"))){
819
820
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A NON NULL VECTOR, FACTOR OR 1D TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
821
822
}else if(all(class(data1) %in% "table")){
if(length(dim(data1)) > 1){
823
824
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data1 ARGUMENT MUST BE A 1D TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
825
826
827
}
}
if( ! any(class(data2) %in% c("logical", "integer", "numeric", "character", "factor", "table"))){
828
829
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A NON NULL VECTOR, FACTOR OR 1D TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
830
831
}else if(all(class(data2) %in% "table")){
if(length(dim(data2)) > 1){
832
833
tempo.cat <- paste0("ERROR IN ", function.name, ": THE data2 ARGUMENT MUST BE A 1D TABLE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
Gael  MILLOT's avatar
Gael MILLOT committed
834
835
836
837
838
}
}
# 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
839
same.class <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
840
class <- NULL
841
same.length <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
842
length <- NULL
843
same.levels <- NULL # not FALSE to deal with no factors
Gael  MILLOT's avatar
Gael MILLOT committed
844
845
846
847
848
levels <- NULL
any.id.levels <- NULL
same.levels.pos1 <- NULL
same.levels.pos2 <- NULL
common.levels <- NULL
849
same.name <- NULL # not FALSE to deal with absence of name
Gael  MILLOT's avatar
Gael MILLOT committed
850
name <- NULL
851
any.id.name <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
852
853
854
same.name.pos1 <- NULL
same.name.pos2 <- NULL
common.names <- NULL
855
any.id.element <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
856
857
858
same.element.pos1 <- NULL
same.element.pos2 <- NULL
common.elements <- NULL
859
860
861
862
863
same.order <- NULL
order1 <- NULL
order2 <- NULL
identical.object <- FALSE
identical.content <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
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
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
889
890
891
same.order <- TRUE
order1 <- order(data1)
order2 <- order(data2)
Gael  MILLOT's avatar
Gael MILLOT committed
892
893
894
identical.object <- TRUE
identical.content <- TRUE
}else{
895
if(identical(class(data1), class(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
896
897
same.class <- TRUE
class <- class(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
898
}
899
if(identical(length(data1), length(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
900
901
same.length<- TRUE
length <- length(data1)
Gael  MILLOT's avatar
Gael MILLOT committed
902
}
Gael  MILLOT's avatar
Gael MILLOT committed
903
if(any(class(data1) %in% "factor") & any(class(data2) %in% "factor")){
904
if(identical(levels(data1), levels(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
905
906
same.levels <- TRUE
levels <- levels(data1)
907
908
}else{
same.levels <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
909
}
Gael  MILLOT's avatar
Gael MILLOT committed
910
911
912
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
913
}
Gael  MILLOT's avatar
Gael MILLOT committed
914
915
916
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
917
}
Gael  MILLOT's avatar
Gael MILLOT committed
918
919
920
921
922
923
924
925
926
927
928
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)))){
929
if(identical(names(data1), names(data2))){
Gael  MILLOT's avatar
Gael MILLOT committed
930
931
same.name <- TRUE
name <- names(data1)
932
933
}else{
same.name <- FALSE
Gael  MILLOT's avatar
Gael MILLOT committed
934
935
936
937
938
939
940
941
942
943
944
945
}
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
946
}
947
948
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
949
950
951
if(any(data1 %in% data2)){
any.id.element <- TRUE
same.element.pos1 <- which(data1 %in% data2)
Gael  MILLOT's avatar
Gael MILLOT committed
952
}
Gael  MILLOT's avatar
Gael MILLOT committed
953
954
955
if(any(data2 %in% data1)){
any.id.element <- TRUE
same.element.pos2 <- which(data2 %in% data1)
Gael  MILLOT's avatar
Gael MILLOT committed
956
}
Gael  MILLOT's avatar
Gael MILLOT committed
957
958
959
if(any.id.element == TRUE){
common.elements <- unique(c(data1[same.element.pos1], data2[same.element.pos2]))
}
960
if(identical(data1, data2)){
Gael  MILLOT's avatar
Gael MILLOT committed
961
identical.content <- TRUE
962
963
964
965
966
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
967
968
}
}
969
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
970
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
971
972
973
}


Gael  MILLOT's avatar
Gael MILLOT committed
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
1000
######## 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
# 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)