cute_little_R_functions.R 897 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
14
15
16
17
18
19
20
21
22
23
24
25
26
## LAST ROUND OF FORMATTING:
# 1) Arguments: one per line
# 2) Description:
# first capital letter everywhere
# AIM
# WARNINGS
# ARGUMENTS
# Update all argument description, saying, character vector, etc, as in fun_gg_boxplot
# RETURN
# Update as in fun_gg_boxplot
# REQUIRED PACKAGES
# REQUIRED FUNCTIONS FROM THE cute PACKAGE
# EXAMPLE
# one example of the example cheet
27
# see http
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# DEBUGGING
# one updated
# 3) Categ (see fun_gg_boxplot for inside code)
# function name
# must have arg.user.setting
# end function name

# required function checking
# see boxplot
# end required function checking

# reserved words (to avoid bugs)
# end reserved words (to avoid bugs)

# arg with no default values
# see boxplot
# end arg with no default values

# argument primary checking
# end argument primary checking

# second round of checking and data preparation
# management of NA arguments
# must have arg.user.setting
# end management of NA arguments

# management of NULL arguments
# end management of NULL arguments

# code that protects set.seed() in the global environment
# end code that protects set.seed() in the global environment

# warning initiation
# end warning initiation

# other checkings
# end other checkings

# reserved word checking
# end reserved word checking
# end second round of checking and data preparation

# package checking
# end package checking
# main code
# output
# if(warn.print == TRUE & ! is.null(warn)){
# end output
# end main code
# 4) example sheet as in fun_gg_boxplot
# 5) test the function with debugging_tools_for_r_dev
# 6) use fun_test()
# 7) write at the beginning of the function:
# todo list check OK
# Check r_debugging_tools-v1.4.R OK
# Check fun_test() (see cute_checks.docx) OK
84
85
# example sheet OK 
# check all and any OK # in if() notably, that does not like NA as a logical result
86
87
88
# clear to go Apollo
# 8) check all(, na.rm = TRUE) and any(, na.rm = TRUE)

89
# fun_mat_fill does not recognize half matrix anymore
90
# package: 
Gael  MILLOT's avatar
Gael MILLOT committed
91
# Templates: https://prettydoc.statr.me/themes.html
92
# http://r-pkgs.had.co.nz/
Gael  MILLOT's avatar
Gael MILLOT committed
93
94
95
# 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
96
97
98
# https://docs.readthedocs.io/en/stable/intro/getting-started-with-sphinx.html
# https://docs.gitlab.com/ee/user/project/pages/
# also register into biotools
99
100
101

# change everywhere: if( ! is.null(arg.check)){

Gael  MILLOT's avatar
Gael MILLOT committed
102
# For heatmap: see https://bioinfo-fr.net/creer-des-heatmaps-a-partir-de-grosses-matrices-en-r
Gael  MILLOT's avatar
Gael MILLOT committed
103

104
105
106
107
# New function: fun_pdf_conc for Rosine code?
# https://stackoverflow.com/questions/17552917/merging-existing-pdf-files-using-r
# https://www.r-bloggers.com/2019/04/join-split-and-compress-pdf-files-with-pdftools/
# https://rdrr.io/cran/staplr/man/staple_pdf.html
Gael  MILLOT's avatar
Gael MILLOT committed
108

Gael  MILLOT's avatar
Gael MILLOT committed
109
################################ OUTLINE ################################
Gael  MILLOT's avatar
Gael MILLOT committed
110
111


Gael  MILLOT's avatar
Gael MILLOT committed
112
113
################ Object analysis    2
######## fun_check() #### check class, type, length, etc., of objects   2
Gael's avatar
Gael committed
114
115
116
117
118
######## 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
119
######## fun_comp_2d() #### comparison of two 2D datasets (row & col names, dimensions, etc.)   22
Gael's avatar
Gael committed
120
######## fun_comp_list() #### comparison of two lists   29
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
######## 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
161
162


Gael  MILLOT's avatar
Gael MILLOT committed
163
################################ FUNCTIONS ################################
Gael  MILLOT's avatar
Gael MILLOT committed
164

Gael  MILLOT's avatar
Gael MILLOT committed
165
166
167
168
169
170
171

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


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


172
# todo list check OK
Gael's avatar
tempo    
Gael committed
173
# Check r_debugging_tools-v1.4.R OK
174
# Check fun_test() 20201107 (see cute_checks.docx) OK
175
176
177
# example sheet OK 
# check all and any OK
# -> clear to go Apollo
Gael's avatar
tempo    
Gael committed
178
fun_check <- function(
179
180
181
182
183
184
185
186
187
188
189
190
191
192
data, 
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, 
data.name = NULL, 
fun.name = NULL
Gael's avatar
tempo    
Gael committed
193
){
194
195
196
197
198
199
200
# 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?
# If options == NULL, then at least class or type or mode or length argument must be non-null
# If options is non-null, then class, type and mode must be NULL, and length can be NULL or specified
# WARNINGS
201
# The function tests what is written in its arguments, even if what is written is incoherent. For instance, fun_check(data = factor(1), class = "factor", mode = "character") will return a problem, whatever the object tested in the data argument, because no object can be class "factor" and mode "character" (factors are class "factor" and mode "numeric"). Of note, length of object of class "environment" is always 0
202
# If the tested object is NULL, then the function will always return a checking problem
203
204
205
206
207
208
209
210
# 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)
# ARGUMENTS
# data: object to test
# 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
# typeof: character string. Either one of the typeof() result or NULL
# mode: character string. Either one of the mode() result (for non-vector object) or NULL
# 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.
211
212
# double.as.integer.allowed: logical. If TRUE, no error is reported in the cheking message 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)
# options: a vector of character strings indicating all the possible option values for the data argument, or NULL
213
214
# all.options.in.data: logical. If TRUE, all of the options must be present at least once in the data argument, and nothing else. If FALSE, some or all of the options must be present in the data argument, and nothing else. Ignored if options is NULL
# na.contain: logical. Can the data argument contain NA?
215
# neg.values: logical. Are negative numeric values authorized? Warning: the default setting is TRUE, meaning that, in that case, no check is performed for the presence of negative values. The neg.values argument is activated only when set to FALSE. In addition, (1) neg.values = FALSE can only be used when class, typeof or mode arguments are not NULL, otherwise return an error message, (2) the presence of negative values is not checked with neg.values = FALSE if the tested object is a factor and the following checking message is returned "OBJECT MUST BE MADE OF NON NEGATIVE VALUES BUT IS A FACTOR"
216
# print: logical. Print the message if $problem is TRUE? Warning: set by default to FALSE, which facilitates the control of the checking message output when using fun_check() inside functions. See the example section
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# data.name: character string indicating the name of the object to test. If NULL, use what is assigned to the data argument for the returned message
# fun.name: character string indicating the name of the function checked (i.e., when fun_check() is used to check the arguments of this function). If non-null, the value of fun.name will be added into the message returned by fun_check()
# RETURN
# A list containing:
# $problem: logical. Is there any problem detected?
# $text: message indicating the details of the problem, or the absence of problem
# $object.name: value of the data.name argument (i.e., name of the checked object if provided, NULL otherwise)
# REQUIRED PACKAGES
# None
# REQUIRED FUNCTIONS FROM THE cute PACKAGE
# None
# EXAMPLE
# test <- matrix(1:3) ; fun_check(data = test, print = TRUE, class = "vector", mode = "numeric")
# see http
# DEBUGGING
232
# data = mean ; class = NULL ; typeof = NULL ; mode = NULL ; length = NULL ; prop = FALSE ; double.as.integer.allowed = FALSE ; options = "a" ; all.options.in.data = FALSE ; na.contain = FALSE ; neg.values = TRUE ; print = TRUE ; data.name = NULL ; fun.name = NULL
233
234
235
236
237
238
239
240
# function name
# no used in this function for the error message, to avoid env colliding
# end function name
# required function checking
# end required function checking
# reserved words
# end reserved words
# fun.name checked first because required next
241
242
243
244
245
246
if( ! is.null(fun.name)){ # I have to use this way to deal with every kind of class for fun.name
if(all(base::class(fun.name) == "character")){ # all() without na.rm -> ok because class(NA) is "logical"
if(base::length(fun.name) != 1){
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 ==
}else if(any(is.na(fun.name))){ # normally no NA with is.na()
247
248
249
tempo.cat <- paste0("ERROR IN fun_check(): NO ARGUMENT EXCEPT data AND options CAN HAVE NA VALUES\nPROBLEMATIC ARGUMENT IS fun.name")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}
250
251
}else{
tempo.cat <- paste0("ERROR IN fun_check(): THE fun.name ARGUMENT MUST BE A CHARACTER VECTOR OF LENGTH 1") # paste(fun.name, collapse = " ") removed here because does not work with objects like function
252
253
254
255
256
257
258
259
260
261
262
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 fun.name checked first because required next
# arg with no default values
if(missing(data)){
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 ==
}
# end arg with no default values
# argument primary checking
Gael's avatar
tempo    
Gael committed
263
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.7/r_debugging_tools-v1.7.R") ; eval(parse(text = str_basic_arg_check_dev)) # activate this line and use the function to check arguments status
264
265
# end argument primary checking
# second round of checking and data preparation
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# management of special classes
basic.class <- c(
"NULL", # because class(NULL) is "NULL". The NULL aspect will be dealt later
"logical", 
"integer", 
"numeric", 
# "complex", 
"character"
# "matrix", 
# "array", 
# "data.frame", 
# "list", 
# "factor", 
# "table", 
# "expression", 
# "name", 
# "symbol", 
# "function", 
# "uneval", 
# "environment", 
# "ggplot2", 
# "ggplot_built", 
# "call"
)
tempo.arg.base <-c( # no names(formals(fun = sys.function(sys.parent(n = 2)))) used with fun_check() to be sure to deal with the correct environment
"class", 
"typeof", 
"mode", 
"length", 
"prop", 
"double.as.integer.allowed", 
"options", 
"all.options.in.data", 
"na.contain", 
"neg.values", 
"print", 
"data.name", 
"fun.name"
)
tempo.class <-list( # no get() used to be sure to deal with the correct environment
base::class(class), 
base::class(typeof), 
base::class(mode), 
base::class(length), 
base::class(prop), 
base::class(double.as.integer.allowed), 
base::class(options), 
base::class(all.options.in.data), 
base::class(na.contain), 
base::class(neg.values), 
base::class(print), 
base::class(data.name), 
base::class(fun.name)
)
tempo <- ! sapply(lapply(tempo.class, FUN = "%in%", basic.class), FUN = all)
if(any(tempo)){
tempo.cat1 <- tempo.arg.base[tempo]
tempo.cat2 <- sapply(tempo.class[tempo], FUN = paste0, collapse = " ")
tempo.sep <- sapply(mapply(" ", max(nchar(tempo.cat1)) - nchar(tempo.cat1) + 3, FUN = rep, SIMPLIFY = FALSE), FUN = paste0, collapse = "")
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", fun.name)), ": ANY ARGUMENT EXCEPT data MUST HAVE A BASIC CLASS\nPROBLEMATIC ARGUMENT", ifelse(base::length(tempo.cat1) > 1, "S", ""), " AND ASSOCIATED CLASS", ifelse(base::length(tempo.cat1) > 1, "ES ARE", " IS"), ":\n", paste0(tempo.cat1, tempo.sep, tempo.cat2, collapse = "\n")) # normally no NA with is.na()
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 management of special classes
329
# management of NA arguments
330
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))){ # normally no NA with is.na()
331
332
tempo <- 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)))]
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", fun.name)), ": NO ARGUMENT EXCEPT data AND options CAN HAVE NA VALUES\nPROBLEMATIC ARGUMENT", ifelse(length(tempo) > 1, "S ARE", " IS"), ":\n", paste(tempo, collapse = "\n")) # normally no NA with is.na()
333
334
335
336
337
338
339
340
341
342
343
344
345
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 management of NA arguments
# management of NULL arguments
tempo.arg <-c(
"prop", 
"double.as.integer.allowed", 
"all.options.in.data", 
"na.contain",
"neg.values",
"print"
)
tempo.log <- sapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = is.null)
346
if(any(tempo.log) == TRUE){ # normally no NA with is.null()
347
tempo.cat <- paste0("ERROR IN fun.check():\n", ifelse(sum(tempo.log, na.rm = TRUE) > 1, "THESE ARGUMENTS", "THIS ARGUMENT"), " CANNOT BE NULL:\n", paste0(tempo.arg[tempo.log], collapse = "\n"))
348
349
350
351
352
353
354
355
356
357
358
359
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 management of NULL arguments
# dealing with logical
# tested below
# end dealing with logical
# code that protects set.seed() in the global environment
# end code that protects set.seed() in the global environment
# warning initiation
# end warning initiation
# other checkings
if( ! is.null(data.name)){
360
if( ! (base::length(data.name) == 1 & all(base::class(data.name) == "character"))){ # all() without na.rm -> ok because class(NA) is "logical"
361
362
363
364
365
366
367
368
369
370
371
372
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
if(is.null(options) & is.null(class) & is.null(typeof) & is.null(mode) &  prop == FALSE & is.null(length)){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
if( ! is.null(options) & ( ! is.null(class) | ! is.null(typeof) | ! is.null(mode) | prop == TRUE)){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
373
if( ! (all(base::class(neg.values) == "logical") & base::length(neg.values) == 1)){ # all() without na.rm -> ok because class(NA) is "logical" 
374
375
376
377
378
379
380
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
if(neg.values == FALSE & is.null(class) & is.null(typeof) & is.null(mode)){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
381
if( ! is.null(class)){ # may add "formula" and "Date" as in https://renenyffenegger.ch/notes/development/languages/R/functions/class
382
383
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", "call") & base::length(class) == 1)){ # length == 1 here because of class(matrix()) since R4.0.0  # all() without na.rm -> ok because class cannot be NA (tested above)
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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\", \"call\"")
384
385
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}
386
if(neg.values == FALSE & ! any(class %in% c("vector", "numeric", "integer", "matrix", "array", "data.frame", "table"))){ # no need of na.rm = TRUE for any() because %in% does not output NA
387
388
389
390
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
391
if( ! is.null(typeof)){ # all the types are here: https://renenyffenegger.ch/notes/development/languages/R/functions/typeof
392
393
if( ! (all(typeof %in% c("logical", "integer", "double", "complex", "character", "list", "expression", "symbol", "closure", "special", "builtin", "environment", "S4", "language")) & base::length(typeof) == 1)){ # "language" is the type of object of class "call" # all() without na.rm -> ok because typeof cannot be NA (tested above)
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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\", \"language\"")
394
395
396
397
398
399
400
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}
if(neg.values == FALSE & ! typeof %in% c("double", "integer")){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
401
if( ! is.null(mode)){ # all the types are here: https://renenyffenegger.ch/notes/development/languages/R/functions/typeof
402
403
if( ! (all(mode %in% c("logical", "numeric", "complex", "character", "list", "expression", "name", "symbol", "function", "environment", "S4", "call")) & base::length(mode) == 1)){ # all() without na.rm -> ok because mode cannot be NA (tested above)
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", fun.name)), ": mode ARGUMENT MUST BE ONE OF THESE VALUE:\n\"logical\", \"numeric\", \"complex\", \"character\", \"list\", \"expression\", \"name\", \"symbol\", \"function\", \"environment\", \"S4\", \"call\"")
404
405
406
407
408
409
410
411
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}
if(neg.values == FALSE & mode != "numeric"){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
if( ! is.null(length)){
412
if( ! (is.numeric(length) & base::length(length) == 1 & ! grepl(length, pattern = "\\."))){
413
414
415
416
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
417
if( ! (is.logical(prop) & base::length(prop) == 1)){ # is.na() already checked for prop
418
419
420
421
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}else if(prop == TRUE){
if( ! is.null(class)){
422
if( ! any(class %in% c("vector", "numeric", "matrix", "array", "data.frame", "table"))){ # no need of na.rm = TRUE for any() because %in% does not output NA
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
if( ! is.null(mode)){
if(mode != "numeric"){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
if( ! is.null(typeof)){
if(typeof != "double"){
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
}
}
440
if( ! (all(base::class(double.as.integer.allowed) == "logical") & base::length(double.as.integer.allowed) == 1)){ # all() without na.rm -> ok because class() never returns NA
441
442
443
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
444
if( ! (is.logical(all.options.in.data) & base::length(all.options.in.data) == 1)){
445
446
447
tempo.cat <- paste0("ERROR IN fun_check()", ifelse(is.null(fun.name), "", paste0(" INSIDE ", 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 ==
}
448
if( ! (all(base::class(na.contain) == "logical") & base::length(na.contain) == 1)){ # all() without na.rm -> ok because class() never returns NA
449
450
451
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 ==
}
452
if( ! (all(base::class(print) == "logical") & base::length(print) == 1)){ # all() without na.rm -> ok because class() never returns NA
453
454
455
456
457
458
459
460
461
462
463
464
465
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 ==
}
# data.name and fun.name tested at the beginning
# end other checkings
# end second round of checking and data preparation
# package checking
# end package checking
# main code
if(is.null(data.name)){
data.name <- deparse(substitute(data))
}
problem <- FALSE
466
text <- paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT")
467
if(( ! is.null(options)) & all(base::typeof(data) == "character")){ # all() without na.rm -> ok because typeof() never returns NA
468
text <- ""
469
if( ! all(data %in% options)){ # no need of na.rm = TRUE for all() because %in% does not output NA
470
problem <- TRUE
471
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " OBJECT MUST BE SOME OF THESE OPTIONS: ", paste(options, collapse = " "), "\nTHE PROBLEMATIC ELEMENTS OF ", data.name, " ARE: ", paste(unique(data[ ! (data %in% options)]), collapse = " "))
472
473
}
if(all.options.in.data == TRUE){
474
if( ! all(options %in% data)){ # no need of na.rm = TRUE for all() because %in% does not output NA
475
problem <- TRUE
476
text <- paste0(ifelse(text == "", "", paste0(text, "\n")), ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " OBJECT 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 = " "))
477
478
479
}
}
if( ! is.null(length)){
480
if(base::length(data) != length){
481
problem <- TRUE
482
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 ", base::length(data))
483
484
485
}
}
if(text == ""){
486
text <- paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT")
487
}
488
489
490
}else if( ! is.null(options)){
problem <- TRUE
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " OBJECT MUST BE SOME OF THESE OPTIONS: ", paste(options, collapse = " "), "\nBUT IS NOT EVEN TYPE CHARACTER")
491
492
493
494
495
}
arg.names <- c("class", "typeof", "mode", "length")
if( ! is.null(class)){
if(class == "matrix"){ # because of class(matric()) since R4.0.0
class <- c("matrix", "array")
496
}else if(class == "factor" & all(base::class(data) %in% c("factor", "ordered"))){ # to deal with ordered factors # all() without na.rm -> ok because class(NA) is "logical"
497
498
499
500
class <- c("factor", "ordered")
}
}
if(is.null(options)){
501
for(i2 in 1:base::length(arg.names)){
502
503
504
if( ! is.null(get(arg.names[i2], env = sys.nframe(), inherit = FALSE))){
# script to execute
tempo.script <- '
Gael  MILLOT's avatar
Gael MILLOT committed
505
problem <- TRUE ;
506
507
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": THE ", data.name, " OBJECT MUST BE ") ;
Gael  MILLOT's avatar
Gael MILLOT committed
508
}else{
Gael  MILLOT's avatar
Gael MILLOT committed
509
text <- paste0(text, " AND ") ; 
Gael  MILLOT's avatar
Gael MILLOT committed
510
}
511
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)})
512
' # no need of na.rm = TRUE for all() because %in% does not output NA
513
# end script to execute
514
if(base::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")))){ # no need of na.rm = TRUE for all() because == does not output NA if no NA in left of ==, which is the case for arg.names # typeof(data) == "double" means no factor allowed
515
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. Warning: na.rm = TRUE required here for all()
516
517
eval(parse(text = tempo.script)) # execute tempo.script
}
518
}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 # no need of na.rm = TRUE for all() because %in% does not output NA # no need of na.rm = TRUE for any() because get get(arg.names) does not contain NA
519
eval(parse(text = tempo.script)) # execute tempo.script
520
}else if(arg.names[i2] == "class" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "vector") & ! (all(base::class(data) %in% "numeric") | all(base::class(data) %in% "integer") | all(base::class(data) %in% "character") | all(base::class(data) %in% "logical"))){ # test class == "vector". No need of na.rm = TRUE for all() because %in% does not output NA # no need of na.rm = TRUE for all() because == does not output NA if no NA in left of ==, which is the case for arg.names
521
eval(parse(text = tempo.script)) # execute tempo.script
522
}else if(arg.names[i2] == "class" & all(get(arg.names[i2], env = sys.nframe(), inherit = FALSE) == "ggplot2") & ! all(base::class(data) %in% c("gg", "ggplot"))){ # test ggplot object # no need of na.rm = TRUE for all() because == does not output NA if no NA in left of ==, which is the case for arg.names # no need of na.rm = TRUE for all() because %in% does not output NA
523
524
525
526
527
eval(parse(text = tempo.script)) # execute tempo.script
}
}
}
}
528
if(prop == TRUE & all(base::typeof(data) == "double")){ # all() without na.rm -> ok because typeof(NA) is "logical"
529
if(is.null(data) | any(data < 0 | data > 1, na.rm = TRUE)){ # works if data is NULL # Warning: na.rm = TRUE required here for any() # typeof(data) == "double" means no factor allowed
530
problem <- TRUE
531
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
532
533
534
535
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
}
536
text <- paste0(text, "THE ", data.name, " OBJECT MUST BE DECIMAL VALUES BETWEEN 0 AND 1")
537
}
538
539
540
541
542
543
544
545
}else if(prop == TRUE){
problem <- TRUE
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
}
text <- paste0(text, "THE ", data.name, " OBJECT MUST BE DECIMAL VALUES BETWEEN 0 AND 1")
546
}
547
if(all(base::class(data) %in% "expression")){ # no need of na.rm = TRUE for all() because %in% does not output NA
548
data <- as.character(data) # to evaluate the presence of NA
Gael  MILLOT's avatar
Gael MILLOT committed
549
}
550
if(na.contain == FALSE & (base::mode(data) %in% c("logical", "numeric", "complex", "character", "list"))){ # before it was ! (class(data) %in% c("function", "environment"))
551
if(any(is.na(data)) == TRUE){ # not on the same line because when data is class envir or function , do not like that # normally no NA with is.na()
552
problem <- TRUE
553
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
554
555
556
557
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
}
558
text <- paste0(text, "THE ", data.name, " OBJECT CONTAINS NA WHILE NOT AUTHORIZED")
559
}
Gael  MILLOT's avatar
Gael MILLOT committed
560
}
561
if(neg.values == FALSE & all(base::mode(data) %in% "numeric") & ! any(base::class(data) %in% "factor")){ # no need of na.rm = TRUE for all() because %in% does not output NA
562
if(any(data < 0, na.rm = TRUE)){ # Warning: na.rm = TRUE required here for any()
563
problem <- TRUE
564
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
565
566
567
568
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
}
569
text <- paste0(text, "THE ", data.name, " OBJECT MUST BE MADE OF NON NEGATIVE NUMERIC VALUES")
570
571
572
573
574
575
576
}
}else if(neg.values == FALSE){
problem <- TRUE
if(identical(text, paste0(ifelse(is.null(fun.name), "", paste0("IN ", fun.name, ": ")), "NO PROBLEM DETECTED FOR THE ", data.name, " OBJECT"))){
text <- paste0(ifelse(is.null(fun.name), "ERROR", paste0("ERROR IN ", fun.name)), ": ")
}else{
text <- paste0(text, " AND ")
577
}
578
text <- paste0(text, "THE ", data.name, " OBJECT MUST BE MADE OF NON NEGATIVE VALUES BUT IS ", ifelse(any(base::class(data) %in% "factor"), "A FACTOR", "NOT EVEN MODE NUMERIC"))
Gael  MILLOT's avatar
Gael MILLOT committed
579
}
Gael  MILLOT's avatar
Gael MILLOT committed
580
if(print == TRUE & problem == TRUE){
581
cat(paste0("\n\n================\n\n", text, "\n\n================\n\n"))
Gael  MILLOT's avatar
Gael MILLOT committed
582
}
583
584
# output
output <- list(problem = problem, text = text, object.name = data.name)
Gael  MILLOT's avatar
Gael MILLOT committed
585
return(output)
586
587
# end output
# end main code
Gael  MILLOT's avatar
Gael MILLOT committed
588
}
Gael  MILLOT's avatar
Gael MILLOT committed
589

590
######## fun_secu() #### verif that local variables are not present in other envs
Gael  MILLOT's avatar
Gael MILLOT committed
591
592
593


fun_secu <- function(pos = 1, name = NULL){
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# AIM
# Verify 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
# 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 the working env will be checked if they also exist in the above envs (Scope of R). If fun_secu() is used in a function, with pos ==1, variables presents in the local env of the function will be checked if they also exist in the above envs (Scope of R, which includes the working environment (Global env))
# name: single character string indicating the name of the function checked. If NULL, fun_secu() checks all the variables of the environment indicated by pos, as explained in the pos argument description. If non-null, fun_secu() checks all the variables presents in the local env of the function will be checked in the above envs (which includes the working environment (Global env)
# RETURN
# A character string of the local variables that match variables in the different environments of the R scope, or NULL if no match
# REQUIRED PACKAGES
# None
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# 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
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
arg.user.setting <- as.list(match.call(expand.dots = FALSE))[-1] # list of the argument settings (excluding default values not provided by the user)
# end function name
# required function checking
if(length(utils::find("fun_check", mode = "function")) == 0){
618
tempo.cat <- paste0("ERROR IN ", function.name, "\nREQUIRED fun_check() FUNCTION IS MISSING IN THE R ENVIRONMENT")
619
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's avatar
tempo    
Gael committed
620
}
621
622
623
624
625
626
627
628
629
630
631
632
# end required function checking
# argument primary checking
# arg with no default values
# end arg with no default values
# using fun_check()
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$object.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)
Gael's avatar
tempo    
Gael committed
633
}
634
635
636
637
if(any(arg.check) == TRUE){
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) #
}
# end using fun_check()
Gael's avatar
tempo    
Gael committed
638
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.7/r_debugging_tools-v1.7.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()
639
640
641
642
643
644
# end argument primary checking
# second round of checking and data preparation
# management of NA arguments
tempo.arg <- names(arg.user.setting) # values provided by the user
tempo.log <- suppressWarnings(sapply(lapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = is.na), FUN = any)) & lapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = length) == 1 # no argument provided by the user can be just NA
if(any(tempo.log) == TRUE){
645
tempo.cat <- paste0("ERROR IN ", function.name, "\n", ifelse(sum(tempo.log, na.rm = TRUE) > 1, "THESE ARGUMENTS", "THIS ARGUMENT"), " CANNOT JUST BE NA:", paste0(tempo.arg[tempo.log], collapse = "\n"))
646
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's avatar
tempo    
Gael committed
647
}
648
649
650
651
652
653
654
# end management of NA arguments
# management of NULL arguments
tempo.arg <- c(
"pos"
)
tempo.log <- sapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = is.null)
if(any(tempo.log) == TRUE){
655
tempo.cat <- paste0("ERROR IN ", function.name, "\n", ifelse(sum(tempo.log, na.rm = TRUE) > 1, "THESE ARGUMENTS", "THIS ARGUMENT"), " CANNOT BE NULL:", paste0(tempo.arg[tempo.log], collapse = "\n"))
656
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's avatar
tempo    
Gael committed
657
}
658
659
660
661
662
663
664
665
666
667
668
669
670
# end management of NULL arguments
# end second round of checking and data preparation
# main code
# 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
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))]
Gael's avatar
tempo    
Gael committed
671
}
672
673
674
675
676
677
678
679
680
681
682
683
# 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
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's avatar
tempo    
Gael committed
684
}
Gael  MILLOT's avatar
Gael MILLOT committed
685
}
686
687
688
689
690
691
if( ! all(sapply(match.list, FUN = is.null))){
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"))
}else{
output <- NULL
}
return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
692
693
694
}


695
######## fun_info() #### broad description of an object
Gael  MILLOT's avatar
Gael MILLOT committed
696
697


698
699
# todo list check OK
# Check r_debugging_tools-v1.4.R OK
700
701
702
703
# Check fun_test() (see cute_checks.docx) OK
# example sheet OK 
# check all and any OK
# -> clear to go Apollo
704
705
706
707
fun_info <- function(
data, 
n = NULL, 
warn.print = TRUE
Gael's avatar
tempo    
Gael committed
708
){
709
710
711
712
713
714
715
716
717
718
# AIM
# Provide a broad description of an object
# WARNINGS
# None
# ARGUMENTS
# data: object to analyse
# n: positive integer value indicating the n first number of elements to display per compartment of the output list (i.e., head(..., n)). Write NULL to return all the elements. Does not apply for the $STRUCTURE compartment output
# warn.print: logical. Print potential warnings at the end of the execution? If FALSE the warning messages are added in the output list as an additional compartment (or NULL if no message).
# RETURN
# A list containing information, depending on the class and type of data. The backbone is generally:
719
# $NAME: name of the object
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# $CLASS: class of the object (class() value)
# $TYPE: type of the object (typeof() value)
# $LENGTH: length of the object (length() value)
# $NA.NB: number of NA and NaN (only for type "logical", "integer", "double", "complex", "character" or "list")
# $HEAD: head of the object (head() value)
# $TAIL: tail of the object (tail() value)
# $DIMENSION: dimension (only for object with dimensions)
# $SUMMARY: object summary (summary() value)
# $STRUCTURE: object structure (str() value)
# $WARNING: warning messages (only if the warn.print argument is FALSE)
# If data is made of numerics, provide also:
# $INF.NB: number of Inf and -Inf
# $RANGE: range after removing Inf and NA
# $SUM: sum after removing Inf and NA
# $MEAN: mean after removing Inf and NA
# If data is a 2D object, provide also:
# $ROW_NAMES: row names
# $COL_NAMES: column names
# If data is a data frame, provide also:
# $COLUMN_TYPE: type of each column (typeof() value)
# If data is a list, provide also:
# $COMPARTMENT_NAMES: names of the comprtments
# $COMPARTMENT_TYPE: type of each compartment (typeof() value)
# REQUIRED PACKAGES
# None
745
# REQUIRED FUNCTIONS FROM THE cute PACKAGE
746
747
# fun_check()
# fun_get_message()
748
# EXAMPLE
749
750
751
# fun_info(data = 1:3)
# see http
# DEBUGGING
752
# mat1 <- matrix(1:3) ; data = env1 ; n = NULL ; warn.print = TRUE # for function debugging
753
754
755
756
757
758
759
760
761
762
763
764
765
# function name
function.name <- paste0(as.list(match.call(expand.dots = FALSE))[[1]], "()")
arg.names <- names(formals(fun = sys.function(sys.parent(n = 2)))) # names of all the arguments
arg.user.setting <- as.list(match.call(expand.dots = FALSE))[-1] # list of the argument settings (excluding default values not provided by the user)
# end function name
# required function checking
req.function <- c(
"fun_check"
)
tempo <- NULL
for(i1 in req.function){
if(length(find(i1, mode = "function")) == 0){
tempo <- c(tempo, i1)
Gael  MILLOT's avatar
Gael MILLOT committed
766
}
Gael  MILLOT's avatar
Gael MILLOT committed
767
}
768
if( ! is.null(tempo)){
769
tempo.cat <- paste0("ERROR IN ", function.name, "\nREQUIRED cute FUNCTION", ifelse(length(tempo) > 1, "S ARE", " IS"), " MISSING IN THE R ENVIRONMENT:\n", paste0(tempo, collapse = "()\n"))
770
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's avatar
tempo    
Gael committed
771
}
772
773
774
775
776
777
778
779
# end required function checking
# reserved words
# end reserved words
# arg with no default values
mandat.args <- c(
"data"
)
tempo <- eval(parse(text = paste0("missing(", paste0(mandat.args, collapse = ") | missing("), ")")))
780
if(any(tempo)){ # normally no NA for missing() output
781
tempo.cat <- paste0("ERROR IN ", function.name, "\nFOLLOWING ARGUMENT", ifelse(length(mandat.args) > 1, "S HAVE", "HAS"), " NO DEFAULT VALUE AND REQUIRE ONE:\n", paste0(mandat.args, collapse = "\n"))
782
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
783
}
784
785
786
787
788
789
790
791
# end arg with no default values
# argument primary 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$object.name))
if( ! is.null(n)){
tempo <- fun_check(data = n, class = "vector", typeof = "integer", length = 1, double.as.integer.allowed = TRUE, fun.name = function.name) ; eval(ee)
Gael's avatar
tempo    
Gael committed
792
793
794
795
}else{
# no fun_check test here, it is just for checked.arg.names
tempo <- fun_check(data = n, class = "vector")
checked.arg.names <- c(checked.arg.names, tempo$object.name)
Gael's avatar
tempo    
Gael committed
796
}
797
tempo <- fun_check(data = warn.print, class = "logical", length = 1, fun.name = function.name) ; eval(ee)
798
if(any(arg.check) == TRUE){ # normally no NA
799
stop(paste0("\n\n================\n\n", paste(text.check[arg.check], collapse = "\n"), "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between == #
Gael's avatar
tempo    
Gael committed
800
}
Gael's avatar
tempo    
Gael committed
801
# source("C:/Users/Gael/Documents/Git_versions_to_use/debugging_tools_for_r_dev-v1.7/r_debugging_tools-v1.7.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()
802
803
804
805
806
# end argument primary checking
# second round of checking and data preparation
# management of NA arguments
tempo.arg <- names(arg.user.setting) # values provided by the user
tempo.log <- suppressWarnings(sapply(lapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = is.na), FUN = any)) & lapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = length) == 1 # no argument provided by the user can be just NA
807
if(any(tempo.log) == TRUE){ # normally no NA because is.na() used here
808
809
810
811
812
813
814
815
816
817
818
tempo.cat <- paste0("ERROR IN ", function.name, ":\n", ifelse(sum(tempo.log, na.rm = TRUE) > 1, "THESE ARGUMENTS\n", "THIS ARGUMENT\n"), paste0(tempo.arg[tempo.log], collapse = "\n"),"\nCANNOT JUST BE NA")
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 management of NA arguments
# management of NULL arguments
tempo.arg <-c(
"data", 
# "n", # because can be NULL
"warn.print"
)
tempo.log <- sapply(lapply(tempo.arg, FUN = get, env = sys.nframe(), inherit = FALSE), FUN = is.null)
819
if(any(tempo.log) == TRUE){# normally no NA with is.null()
820
821
822
823
824
825
826
827
tempo.cat <- paste0("ERROR IN ", function.name, ":\n", ifelse(sum(tempo.log, na.rm = TRUE) > 1, "THESE ARGUMENTS\n", "THIS ARGUMENT\n"), paste0(tempo.arg[tempo.log], collapse = "\n"),"\nCANNOT BE 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 ==
}
# end management of NULL arguments
# code that protects set.seed() in the global environment
# end code that protects set.seed() in the global environment
# warning initiation
ini.warning.length <- options()$warning.length
828
options(warning.length = 8170)
829
830
831
832
833
834
835
836
warn <- NULL
# warn.count <- 0 # not required
# end warning initiation
# other checkings
if( ! is.null(n)){
if(n < 1){
tempo.cat <- paste0("ERROR IN ", function.name, ": n ARGUMENT MUST BE A POSITIVE AND NON NULL INTEGER")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
837
}else if(is.finite(n)){
838
839
840
841
# warn.count <- warn.count + 1
tempo.warn <- paste0("SOME COMPARTMENTS CAN BE TRUNCATED (n ARGUMENT IS ", n, ")")
warn <- paste0(ifelse(is.null(warn), tempo.warn, paste0(warn, "\n\n", tempo.warn)))
}
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
# end other checkings
# reserved word checking
# end reserved word checking
# end second round of checking and data preparation
# package checking
# end package checking
# main code
# new environment
env.name <- paste0("env", as.numeric(Sys.time()))
if(exists(env.name, where = -1)){ # verify if still ok when fun_info() is inside a function
tempo.cat <- paste0("ERROR IN ", function.name, ": ENVIRONMENT env.name ALREADY EXISTS. PLEASE RERUN ONCE")
stop(paste0("\n\n================\n\n", tempo.cat, "\n\n================\n\n"), call. = FALSE) # == in stop() to be able to add several messages between ==
}else{
assign(env.name, new.env())
assign("data", data, envir = get(env.name, env = sys.nframe(), inherit = FALSE)) # data assigned in a new envir for test
}
# end new environment
data.name <- deparse(substitute(data))
output <- list("NAME" = data.name)
tempo.try.error <- fun_get_message(data = "class(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- list("CLASS" = class(data))
output <- c(output, tempo)
}
tempo.try.error <- fun_get_message(data = "typeof(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- list("TYPE" = typeof(data))
output <- c(output, tempo)
}
tempo.try.error <- fun_get_message(data = "length(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- list("LENGTH" = length(data))
output <- c(output, tempo)
}
877
if(all(typeof(data) %in% c("integer", "numeric", "double")) & ! any(class(data) %in% "factor")){ # all() without na.rm -> ok because typeof(NA) is "logical" # any() without na.rm -> ok because class(NA) is "logical"
878
879
880
881
882
883
884
885
886
tempo <- list("INF.NB" = sum(is.infinite(data)))
output <- c(output, tempo)
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)
}
887
if(all(typeof(data) %in% c("logical", "integer", "double", "complex", "character", "list"))){ # all() without na.rm -> ok because typeof(NA) is "logical"
888
889
890
891
892
893
tempo.try.error <- fun_get_message(data = "is.na(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- list("NA.NB" = sum(is.na(data)))
output <- c(output, tempo)
}
}
894
895
tempo.try.error <- fun_get_message(data = "head(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
896
897
tempo <- list("HEAD" = head(data))
output <- c(output, tempo)
898
tempo <- list("TAIL" = tail(data)) # no reason that tail() does not work if head() works
899
output <- c(output, tempo)
900
}
901
902
903
904
905
906
907
908
909
910
tempo.try.error <- fun_get_message(data = "dim(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
if(length(dim(data)) > 0){
tempo <- list("DIMENSION" = dim(data))
if(length(tempo[[1]]) == 2){
names(tempo[[1]]) <- c("NROW", "NCOL")
}
output <- c(output, tempo)
}
}
911
if(all(class(data) == "data.frame") | all(class(data) %in% c("matrix", "array")) | all(class(data) == "table")){ # all() without na.rm -> ok because typeof(NA) is "logical"
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
if(length(dim(data)) > 1){ # to avoid 1D table
tempo <- list("ROW_NAMES" = dimnames(data)[[1]])
output <- c(output, tempo)
tempo <- list("COLUM_NAMES" = dimnames(data)[[2]])
output <- c(output, tempo)
}
}
tempo.try.error <- fun_get_message(data = "summary(data)", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- list("SUMMARY" = summary(data))
output <- c(output, tempo)
}
tempo.try.error <- fun_get_message(data = "noquote(matrix(capture.output(str(data))))", kind = "error", header = FALSE, env = get(env.name, env = sys.nframe(), inherit = FALSE))
if(is.null(tempo.try.error)){
tempo <- capture.output(str(data))
tempo <- list("STRUCTURE" = noquote(matrix(tempo, dimnames = list(rep("", length(tempo)), "")))) # str() print automatically, ls.str() not but does not give the order of the data.frame
output <- c(output, tempo)
}
930
if(all(class(data) == "data.frame")){ # all() without na.rm -> ok because class(NA) is "logical"
931
tempo <- list("COLUMN_TYPE" = sapply(data, FUN = "typeof"))
932
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") # any() without na.rm -> ok because class(NA) is "logical"
933
tempo.class <- sapply(data, FUN = "class")
934
if(any(unlist(tempo.class) %in% "ordered")){ # any() without na.rm -> ok because class(NA) is "logical"
935
936
937
938
939
940
941
942
tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor"
}else{
tempo2 <- unlist(tempo.class)
}
tempo[["COLUMN_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
}
output <- c(output, tempo)
}
943
if(all(class(data) == "list")){ # all() without na.rm -> ok because class(NA) is "logical"
944
945
946
tempo <- list("COMPARTMENT_NAMES" = names(data))
output <- c(output, tempo)
tempo <- list("COMPARTMENT_TYPE" = sapply(data, FUN = "typeof"))
947
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")  # any() without na.rm -> ok because class(NA) is "logical"
948
tempo.class <- sapply(data, FUN = "class")
949
if(any(unlist(tempo.class) %in% "ordered")){ # any() without na.rm -> ok because class(NA) is "logical"
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
tempo2 <- sapply(tempo.class, paste, collapse = " ") # paste the "ordered" factor" in "ordered factor"
}else{
tempo2 <- unlist(tempo.class)
}
tempo[["COMPARTMENT_TYPE"]][grepl(x = tempo2, pattern = "factor")] <- tempo2[grepl(x = tempo2, pattern = "factor")]
}
output <- c(output, tempo)
}
if( ! is.null(n)){
output[names(output) != "STRUCTURE"] <- lapply(X = output[names(output) != "STRUCTURE"], FUN = head, n = n, simplify = FALSE)
}
# output
if(warn.print == FALSE){
output <- c(output, WARNING = warn)
}else if(warn.print == TRUE & ! is.null(warn)){
on.exit(warning(paste0("FROM ", function.name, ":\n\n", warn), call. = FALSE))
}
967
on.exit(exp = options(warning.length = ini.warning.length), add = TRUE)
968
969
970
return(output)
# end output
# end main code
Gael's avatar
tempo    
Gael committed
971
}
972
973


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


977
978
979
980
fun_head <- function(
data1, 
n = 6, 
side = "l"
981
){
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_check()
# 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