cute_little_R_functions.R 195 KB
Newer Older
Gael  MILLOT's avatar
Gael MILLOT committed
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
            active.wind.nb <- 0
        }
        if(Sys.info()["sysname"] == "Windows"){ # Note that .Platform$OS.type() only says "unix" for macOS and Linux and "Windows" for Windows
            windows()
            ini.par <- par(no.readonly = TRUE)  # to recover the initial graphical parameters if required (reset). BEWARE: this command alone opens a pdf of GUI window if no window already opened. But here, protected with the code bacause always a tempo window opened
            invisible(dev.off()) # close the new window
        }else if(Sys.info()["sysname"] == "Linux"){
            if(file.exists(paste0(getwd(), "/Rplots.pdf"))){
                tempo.cat <- paste0("\n\n================\n\nERROR IN fun_graph_param_prior_plot(): THIS FUNCTION CANNOT BE USED ON LINUX WITH param.reinitial SET TO TRUE IF A Rplots.pdf FILE ALREADY EXISTS HERE: ", getwd(), "\n\n================\n\n")
                stop(tempo.cat)
            }else{
                open.fail <- suppressWarnings(try(X11(), silent = TRUE))[] # try to open a X11 window. If open.fail == NULL, no problem, meaning that the X11 window is opened. If open.fail != NULL, a pdf can be opened here paste0(getwd(), "/Rplots.pdf")
                if(is.null(open.fail)){
                    ini.par <- par(no.readonly = TRUE) # to recover the initial graphical parameters if required (reset). BEWARE: this command alone opens a pdf of GUI window if no window already opened. But here, protected with the code bacause always a tempo window opened
                    invisible(dev.off()) # close the new window
                }else if(file.exists(paste0(getwd(), "/Rplots.pdf"))){
                    ini.par <- par(no.readonly = TRUE) # to recover the initial graphical parameters if required (reset). BEWARE: this command alone opens a pdf of GUI window if no window already opened. But here, protected with the code bacause always a tempo window opened
                    invisible(dev.off()) # close the new window
                    file.remove(paste0(getwd(), "/Rplots.pdf")) # remove the pdf file
                }else{
                    tempo.cat <- ("\n\n================\n\nPROBLEM IN fun_graph_param_prior_plot(): THIS FUNCTION CANNOT OPEN GUI ON LINUX OR NON MACOS UNIX SYSTEM (X GRAPHIC INTERFACE HAS TO BE SET).\nTO OVERCOME THIS, PLEASE USE PDF GRAPHIC INTERFACES AND RERUN\n\n================\n\n")
                    stop(tempo.cat)
                }
            }
        }else{ # macOS
            quartz()
            ini.par <- par(no.readonly = TRUE)  # to recover the initial graphical parameters if required (reset). BEWARE: this command alone opens a pdf of GUI window if no window already opened. But here, protected with the code bacause always a tempo window opened)
            invisible(dev.off()) # close the new window
        }
        if( ! all(names(dev.cur()) == "null device")){
            dev.set(active.wind.nb) # go back to the active windows if exists
            par(ini.par) # apply the initial par to current window
        }
    }
    if(remove.x.axis == TRUE){
        par(xaxt = "n") # suppress the y-axis label
    }else{
        par(xaxt = "s")
    }
    if(remove.y.axis == TRUE){
        par(yaxt = "n") # suppress the y-axis label
    }else{
        par(yaxt = "s")
    }
    if(std.x.range == TRUE){
        par(xaxs = "i")
    }else{
        par(xaxs = "r")
    }
    if(std.y.range == TRUE){
        par(yaxs = "i")
    }else{
        par(yaxs = "r")
    }
    par(mai = c(down.space, left.space, up.space, right.space), ann = ! remove.label, las = orient, mgp = c(dist.legend/0.2, 1, 0), xpd = display.extend, bty= box.type, cex.lab = amplif.label, cex.axis = amplif.axis)
    par(tcl = -par()$mgp[2] * tick.length) # tcl gives the length of the ticks as proportion of line text, knowing that mgp is in text lines. So the main ticks are a 0.5 of the distance of the axis numbers by default. The sign provides the side of the tick (negative for outside of the plot region)
    if(xlog.scale == TRUE){
        par(xaxt = "n", xlog = TRUE) # suppress the x-axis label
    }else{
        par(xlog = FALSE)
    }
    if(ylog.scale == TRUE){
        par(yaxt = "n", ylog = TRUE) # suppress the y-axis label
    }else{
        par(ylog = FALSE)
    }
    if(return.par == TRUE){
        tempo.par <- par()
        return(tempo.par)
    }
Gael  MILLOT's avatar
Gael MILLOT committed
2071
2072
2073
2074
2075
2076
2077
}


######## fun_feature_post_plot() #### Graph param after plotting


# Check OK: clear to go Apollo
Gael  MILLOT's avatar
Gael MILLOT committed
2078
fun_feature_post_plot <- function(x.side = 0, x.log.scale = FALSE, x.categ = NULL, x.categ.pos = NULL, x.lab = "", x.axis.magnific = 1.5, x.label.magnific = 1.5, x.dist.legend = 0.5, x.nb.inter.tick = 1, y.side = 0, y.log.scale = FALSE, y.categ = NULL, y.categ.pos = NULL, y.lab = "", y.axis.magnific = 1.5, y.label.magnific = 1.5, y.dist.legend = 0.5, y.nb.inter.tick = 1, text.angle = 90, tick.length = 0.5, sec.tick.length = 0.3, bg.color = NULL, grid.lwd = NULL, grid.col = "white", corner.text = "", magnific.corner.text = 1, just.label.add = FALSE, par.reset = FALSE, custom.par = NULL){
Gael  MILLOT's avatar
Gael MILLOT committed
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
    # AIM:
    # redesign axis. If x.side = 0, y.side = 0, the function just adds text at topright of the graph and reset par() for next graphics and provides outputs (see below)
    # provide also positions for legend or additional text on the graph
    # use fun_graph_param_prior_plot() before this function for initial inactivation of the axis drawings
    # REQUIRED FUNCTIONS
    # fun_param_check()
    # fun_open_window() to reinitialize graph parameters if par.reset = TRUE and custom.par = NULL
    # ARGUMENTS
    # x.side: axis at the bottom (1) or top (3) of the region figure. Write 0 for no change
    # x.log.scale: Log scale for the x-axis? Either TRUE or FALSE
    # x.categ: character vector representing the classes (levels()) to specify when the x-axis is qualititative(stripchart, boxplot)
    # x.categ.pos: position of the classes names (numeric vector of identical length than x.categ). If left NULL, this will be 1:length(levels())
    # x.lab: label of the x-axis. If x.side == 0 and x.lab != "", then x.lab is printed
    # x.axis.magnific: increase or decrease the value to increase or decrease the size of the x axis numbers. Also control the size of displayed categories
    # x.label.magnific: increase or decrease the value to increase or decrease the size of the x axis legend
    # x.dist.legend: increase the number to move x-axis legends away in inches (first number of mgp argument of par() but in inches)
    # x.nb.inter.tick: number of secondary ticks between main ticks on x-axis (only if not log scale). 0 means no secondary ticks
    # y.side: axis at the left (2) or right (4) of the region figure. Write 0 for no change
    # y.log.scale: Log scale for the y-axis? Either TRUE or FALSE
    # y.categ: classes (levels()) to specify when the y-axis is qualititative(stripchart, boxplot)
    # y.categ.pos: position of the classes names (numeric vector of identical length than y.categ). If left NULL, this will be 1:length(levels())
    # y.lab: label of the y-axis. If y.side == 0 and y.lab != "", then y.lab is printed
    # y.axis.magnific: increase or decrease the value to increase or decrease the size of the y axis numbers. Also control the size of displayed categories
    # y.label.magnific: increase or decrease the value to increase or decrease the size of the y axis legend
    # y.dist.legend: increase the number to move y-axis legends away in inches (first number of mgp argument of par() but in inches)
    # y.nb.inter.tick: number of secondary ticks between main ticks on y-axis (only if not log scale). 0 means non secondary ticks
    # text.angle: angle of the text when axis is qualitative
    # tick.length: length of the main ticks (1 means complete the distance between the plot region and the axis numbers, 0.5 means half the length, etc., 0 for no ticks)
    # sec.tick.length: length of the secondary ticks (1 means complete the distance between the plot region and the axis numbers, 0.5 means half the length, etc., 0 for no ticks)
    # bg.color: background color of the plot region. NULL for no color. BEWARE: cover/hide an existing plot !
    # grid.lwd: if non NULL, activate the grid line (specify the line width)
    # grid.col: grid line color (only if grid.lwd non NULL)
    # corner.text: text to add at the top right corner of the window
    # magnific.corner.text: increase or decrease the size of the text
    # par.reset: to reset all the graphics parameters. BEWARE: TRUE can generate display problems, mainly in graphic devices with multiple figure regions
    # just.label.add: just add axis labels (legend)? Either TRUE or FALSE. If TRUE, at least (x.side == 0 & x.lab != "") or (y.side == 0 & y.lab != "") must be set to display the corresponding x.lab or y.lab
    # custom.par: list that provides the parameters that reset all the graphics parameters. BEWARE: if NULL and par.reset == TRUE, the default par() parameters are used
    # RETURN
    # a list containing: 
    # $x.mid.left.dev.region: middle of the left margin of the device region, in coordinates of the x-axis
    # $x.left.dev.region: left side of the left margin (including the potential margin of the device region), in coordinates of the x-axis
    # $x.mid.right.dev.region: middle of the right margin of the device region, in coordinates of the x-axis
    # $x.right.dev.region: right side of the right margin (including the potential margin of the device region), in coordinates of the x-axis
    # $x.mid.left.fig.region: middle of the left margin of the figure region, in coordinates of the x-axis
    # $x.left.fig.region: left side of the left margin, in coordinates of the x-axis
    # $x.mid.right.fig.region: middle of the right margin of the figure region, in coordinates of the x-axis
    # $x.right.fig.region: right side of the right margin, in coordinates of the x-axis
    # $x.left.plot.region: left side of the plot region, in coordinates of the x-axis
    # $x.right.plot.region: right side of the plot region, in coordinates of the x-axis
    # $x.mid.plot.region: middle of the plot region, in coordinates of the x-axis
    # $y.mid.bottom.dev.region: middle of the bottom margin of the device region, in coordinates of the y-axis
    # $y.bottom.dev.region: bottom side of the bottom margin (including the potential margin of the device region), in coordinates of the y-axis
    # $y.mid.top.dev.region: middle of the top margin of the device region, in coordinates of the y-axis
    # $y.top.dev.region: top side of the top margin (including the potential margin of the device region), in coordinates of the y-axis
    # $y.mid.bottom.fig.region: middle of the bottom margin of the figure region, in coordinates of the y-axis
    # $y.bottom.fig.region: bottom of the bottom margin of the figure region, in coordinates of the y-axis
    # $y.mid.top.fig.region: middle of the top margin of the figure region, in coordinates of the y-axis
    # $y.top.fig.region: top of the top margin of the figure region, in coordinates of the y-axis
    # $y.top.plot.region: top of the plot region, in coordinates of the y-axis
    # $y.bottom.plot.region: bottom of the plot region, in coordinates of the y-axis
    # $y.mid.plot.region: middle of the plot region, in coordinates of the y-axis
    # $text: warning text
    # EXAMPLES
    # Example of log axis with log y-axis and unmodified x-axis:
    # prior.par <- fun_graph_param_prior_plot(param.reinitial = TRUE, xlog.scale = FALSE, ylog.scale = TRUE, remove.label = TRUE, remove.x.axis = FALSE, remove.y.axis = TRUE, down.space = 1, left.space = 1, up.space = 1, right.space = 1, orient = 1, dist.legend = 0.5, tick.length = 0.5, box.type = "n", amplif.label = 1, amplif.axis = 1, display.extend = FALSE, return.par = TRUE) ; plot(1:100, log = "y") ; fun_feature_post_plot(y.side = 2, y.log.scale = prior.par$ylog, x.lab = "Values", y.lab = "TEST", y.axis.magnific = 1.25, y.label.magnific = 1.5, y.dist.legend = 0.7, just.label.add = ! prior.par$ann)
    # Example of log axis with redrawn x-axis and y-axis:
    # prior.par <- fun_graph_param_prior_plot(param.reinitial = TRUE) ; plot(1:100) ; fun_feature_post_plot(x.side = 1, x.lab = "Values", y.side = 2, y.lab = "TEST", y.axis.magnific = 1, y.label.magnific = 2, y.dist.legend = 0.6)
    # example with margins in the device region:
    # windows(5,5) ; par(mai=c(0.5,0.5,0.5,0.5), omi = c(0.25,0.25,1,0.25), xaxs = "i", yaxs = "i") ; plot(0:10) ; a <- fun_feature_post_plot(x.side = 0, y.side = 0) ; x <- c(a$x.mid.left.dev.region, a$x.left.dev.region, a$x.mid.right.dev.region, a$x.right.dev.region, a$x.mid.left.fig.region, a$x.left.fig.region, a$x.mid.right.fig.region, a$x.right.fig.region, a$x.right.plot.region, a$x.left.plot.region, a$x.mid.plot.region) ; y <- c(a$y.mid.bottom.dev.region, a$y.bottom.dev.region, a$y.mid.top.dev.region, a$y.top.dev.region, a$y.mid.bottom.fig.region, a$y.bottom.fig.region, a$y.mid.top.fig.region, a$y.top.fig.region, a$y.top.plot.region, a$y.bottom.plot.region, a$y.mid.plot.region) ; par(xpd = NA) ; points(x = rep(5, length(y)), y = y, pch = 16, col = "red") ; text(x = rep(5, length(y)), y = y, c("y.mid.bottom.dev.region", "y.bottom.dev.region", "y.mid.top.dev.region", "y.top.dev.region", "y.mid.bottom.fig.region", "y.bottom.fig.region", "y.mid.top.fig.region", "y.top.fig.region", "y.top.plot.region", "y.bottom.plot.region", "y.mid.plot.region"), cex = 0.65, col = grey(0.25)) ; points(y = rep(5, length(x)), x = x, pch = 16, col = "blue") ; text(y = rep(5, length(x)), x = x, c("x.mid.left.dev.region", "x.left.dev.region", "x.mid.right.dev.region", "x.right.dev.region", "x.mid.left.fig.region", "x.left.fig.region", "x.mid.right.fig.region", "x.right.fig.region", "x.right.plot.region", "x.left.plot.region", "x.mid.plot.region"), cex = 0.65, srt = 90, col = grey(0.25))
    # DEBUGGING
    # x.side = 0 ; x.log.scale = FALSE ; x.categ = NULL ; x.categ.pos = NULL ; x.lab = "" ; x.axis.magnific = 1.5 ; x.label.magnific = 1.5 ; x.dist.legend = 1 ; x.nb.inter.tick = 1 ; y.side = 0 ; y.log.scale = FALSE ; y.categ = NULL ; y.categ.pos = NULL ; y.lab = "" ; y.axis.magnific = 1.5 ; y.label.magnific = 1.5 ; y.dist.legend = 0.7 ; y.nb.inter.tick = 1 ; text.angle = 90 ; tick.length = 0.5 ; sec.tick.length = 0.3 ; bg.color = NULL ; grid.lwd = NULL ; grid.col = "white" ; corner.text = "" ; magnific.corner.text = 1 ; just.label.add = FALSE ; par.reset = FALSE ; custom.par = NULL # for function debugging
    # required function checking
    if(length(find("fun_param_check", mode = "function")) == 0){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_feature_post_plot(): REQUIRED fun_param_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
        stop(tempo.cat)
    }
    if(length(find("fun_open_window", mode = "function")) == 0){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_feature_post_plot(): REQUIRED fun_open_window() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
        stop(tempo.cat)
    }
    # end required function checking
    # argument checking
    arg.check <- NULL # for function debbuging
    checked.arg.names <- NULL # for function debbuging
    ee <- expression(arg.check <- c(arg.check, tempo$problem) , checked.arg.names <- c(checked.arg.names, tempo$param.name))
    tempo <- fun_param_check(data = x.side, options = c(0, 1, 3), length = 1) ; eval(ee)
    tempo <- fun_param_check(data = x.log.scale, class = "logical", length = 1) ; eval(ee)
    if( ! is.null(x.categ)){
        tempo <- fun_param_check(data = x.categ, class = "character", na.contain = TRUE) ; eval(ee)
    }
    if( ! is.null(x.categ.pos)){
Gael  MILLOT's avatar
Gael MILLOT committed
2170
        tempo <- fun_param_check(data = x.categ.pos, mode = "numeric") ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2171
2172
    }
    tempo <- fun_param_check(data = x.lab, class = "character", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2173
2174
2175
    tempo <- fun_param_check(data = x.axis.magnific, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = x.label.magnific, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = x.dist.legend, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2176
2177
2178
2179
2180
2181
2182
    tempo <- fun_param_check(data = x.nb.inter.tick, typeof = "integer", length = 1, double.as.integer.allowed = TRUE) ; eval(ee)
    tempo <- fun_param_check(data = y.side, options = c(0, 2, 4), length = 1) ; eval(ee)
    tempo <- fun_param_check(data = y.log.scale, class = "logical", length = 1) ; eval(ee)
    if( ! is.null(y.categ)){
        tempo <- fun_param_check(data = y.categ, class = "character", na.contain = TRUE) ; eval(ee)
    }
    if( ! is.null(y.categ.pos)){
Gael  MILLOT's avatar
Gael MILLOT committed
2183
        tempo <- fun_param_check(data = y.categ.pos, mode = "numeric") ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2184
2185
    }
    tempo <- fun_param_check(data = y.lab, class = "character", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2186
2187
2188
    tempo <- fun_param_check(data = y.axis.magnific, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = y.label.magnific, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = y.dist.legend, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2189
    tempo <- fun_param_check(data = y.nb.inter.tick, typeof = "integer", length = 1, double.as.integer.allowed = TRUE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2190
2191
2192
    tempo <- fun_param_check(data = text.angle, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = tick.length, mode = "numeric", length = 1, prop = TRUE) ; eval(ee)
    tempo <- fun_param_check(data = sec.tick.length, mode = "numeric", length = 1, prop = TRUE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2193
2194
2195
2196
2197
2198
2199
2200
    if( ! is.null(bg.color)){
        tempo <- fun_param_check(data = bg.color, class = "character", length = 1) ; eval(ee)
        if( ! (bg.color %in% colors() | grepl(pattern = "^#", bg.color))){ # check color
            tempo.cat <- paste0("\n\n================\n\nERROR IN fun_feature_post_plot(): bg.color ARGUMENT MUST BE A HEXADECIMAL COLOR VECTOR STARTING BY # OR A COLOR NAME GIVEN BY colors()\n\n================\n\n")
            stop(tempo.cat)
        }
    }
    if( ! is.null(grid.lwd)){
Gael  MILLOT's avatar
Gael MILLOT committed
2201
        tempo <- fun_param_check(data = grid.lwd, mode = "numeric", neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2202
2203
2204
2205
2206
2207
2208
2209
2210
    }
    if( ! is.null(grid.col)){
        tempo <- fun_param_check(data = grid.col, class = "character", length = 1) ; eval(ee)
        if( ! (grid.col %in% colors() | grepl(pattern = "^#", grid.col))){ # check color
            tempo.cat <- paste0("\n\n================\n\nERROR IN fun_feature_post_plot(): grid.col ARGUMENT MUST BE A HEXADECIMAL COLOR VECTOR STARTING BY # OR A COLOR NAME GIVEN BY colors()\n\n================\n\n")
            stop(tempo.cat)
        }
    }
    tempo <- fun_param_check(data = corner.text, class = "character", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2211
    tempo <- fun_param_check(data = magnific.corner.text, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
    tempo <- fun_param_check(data = just.label.add, class = "logical", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = par.reset, class = "logical", length = 1) ; eval(ee)
    if( ! is.null(custom.par)){
        tempo <- fun_param_check(data = custom.par, typeof = "list", length = 1) ; eval(ee)
    }
    if(any(arg.check) == TRUE){
        stop() # nothing else because print = TRUE by default in fun_param_check()
    }
    # 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_param_check_dev)) # activate this line and use the function to check arguments status and if they have been checked using fun_param_check()
    # end argument checking
    text <- NULL
    par(tcl = -par()$mgp[2] * tick.length)
    if(x.log.scale == TRUE){
        grid.coord.x <- c(10^par("usr")[1], 10^par("usr")[2])
    }else{
        grid.coord.x <- c(par("usr")[1], par("usr")[2])
    }
    if(y.log.scale == TRUE){
        grid.coord.y <- c(10^par("usr")[3], 10^par("usr")[4])
    }else{
        grid.coord.y <- c(par("usr")[3], par("usr")[4])
    }
    if( ! is.null(bg.color)){
        rect(grid.coord.x[1], grid.coord.y[1], grid.coord.x[2], grid.coord.y[2], col = bg.color, border = NA)
    }
    if( ! is.null(grid.lwd)){
        grid(nx = NA, ny = NULL, col = grid.col, lty = 1, lwd = grid.lwd)
    }
    if(x.log.scale == TRUE){
        x.mid.left.dev.region <- 10^(par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * par("omd")[1] / 2) # in x coordinates, to position axis labeling at the bottom of the graph (according to x scale)
        x.left.dev.region <- 10^(par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * par("omd")[1]) # in x coordinates
        x.mid.right.dev.region <- 10^(par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) + ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * (1 - par("omd")[2]) / 2) # in x coordinates, to position axis labeling at the top of the graph (according to x scale)
        x.right.dev.region <- 10^(par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) + ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * (1 - par("omd")[2])) # in x coordinates
        x.mid.left.fig.region <- 10^(par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] / 2) # in x coordinates, to position axis labeling at the bottom of the graph (according to x scale)
        x.left.fig.region <- 10^(par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1]) # in x coordinates
        x.mid.right.fig.region <- 10^(par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) / 2) # in x coordinates, to position axis labeling at the top of the graph (according to x scale)
        x.right.fig.region <- 10^(par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2])) # in x coordinates
        x.left.plot.region <- 10^par("usr")[1] # in x coordinates, left of the plot region (according to x scale)
        x.right.plot.region <- 10^par("usr")[2] # in x coordinates, right of the plot region (according to x scale)
        x.mid.plot.region <- 10^((par("usr")[2] + par("usr")[1]) / 2) # in x coordinates, right of the plot region (according to x scale)
    }else{
        x.mid.left.dev.region <- (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * par("omd")[1] / 2) # in x coordinates, to position axis labeling at the bottom of the graph (according to x scale)
        x.left.dev.region <- (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * par("omd")[1]) # in x coordinates
        x.mid.right.dev.region <- (par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) + ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * (1 - par("omd")[2]) / 2) # in x coordinates, to position axis labeling at the top of the graph (according to x scale)
        x.right.dev.region <- (par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) + ((par("usr")[2] -  par("usr")[1]) / ((par("omd")[2] - par("omd")[1]) * (par("plt")[2] - par("plt")[1]))) * (1 - par("omd")[2])) # in x coordinates
        x.mid.left.fig.region <- (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] / 2) # in x coordinates, to position axis labeling at the bottom of the graph (according to x scale)
        x.left.fig.region <- (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1]) # in x coordinates
        x.mid.right.fig.region <- (par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2]) / 2) # in x coordinates, to position axis labeling at the top of the graph (according to x scale)
        x.right.fig.region <- (par("usr")[2] + ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * (1 - par("plt")[2])) # in x coordinates
        x.left.plot.region <- par("usr")[1] # in x coordinates, left of the plot region (according to x scale)
        x.right.plot.region <- par("usr")[2] # in x coordinates, right of the plot region (according to x scale)
        x.mid.plot.region <- (par("usr")[2] + par("usr")[1]) / 2 # in x coordinates, right of the plot region (according to x scale)
    }
    if(y.log.scale == TRUE){
        y.mid.bottom.dev.region <- 10^(par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] - ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (par("omd")[3] / 2)) # in y coordinates, to position axis labeling at the bottom of the graph (according to y scale). Ex mid.bottom.space
        y.bottom.dev.region <- 10^(par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] - ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * par("omd")[3]) # in y coordinates
        y.mid.top.dev.region <- 10^(par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (1 - par("omd")[4]) / 2) # in y coordinates, to position axis labeling at the top of the graph (according to y scale). Ex mid.top.space
        y.top.dev.region <- 10^(par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (1 - par("omd")[4])) # in y coordinates
        y.mid.bottom.fig.region <- 10^(par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] / 2) # in y coordinates, to position axis labeling at the bottom of the graph (according to y scale). Ex mid.bottom.space
        y.bottom.fig.region <- 10^(par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3]) # in y coordinates
        y.mid.top.fig.region <- 10^(par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) / 2) # in y coordinates, to position axis labeling at the top of the graph (according to y scale). Ex mid.top.space
        y.top.fig.region <- 10^(par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4])) # in y coordinates
        y.top.plot.region <- 10^par("usr")[4] # in y coordinates, top of the plot region (according to y scale)
        y.bottom.plot.region <- 10^par("usr")[3] # in y coordinates, bottom of the plot region (according to y scale)
        y.mid.plot.region <- (par("usr")[3] + par("usr")[4]) / 2 # in x coordinates, right of the plot region (according to x scale)
    }else{
        y.mid.bottom.dev.region <- (par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] - ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (par("omd")[3] / 2)) # in y coordinates, to position axis labeling at the bottom of the graph (according to y scale). Ex mid.bottom.space
        y.bottom.dev.region <- (par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] - ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * par("omd")[3]) # in y coordinates
        y.mid.top.dev.region <- (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (1 - par("omd")[4]) / 2) # in y coordinates, to position axis labeling at the top of the graph (according to y scale). Ex mid.top.space
        y.top.dev.region <- (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / ((par("omd")[4] - par("omd")[3]) * (par("plt")[4] - par("plt")[3]))) * (1 - par("omd")[4])) # in y coordinates
        y.mid.bottom.fig.region <- (par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3] / 2) # in y coordinates, to position axis labeling at the bottom of the graph (according to y scale). Ex mid.bottom.space
        y.bottom.fig.region <- (par("usr")[3] - ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * par("plt")[3]) # in y coordinates
        y.mid.top.fig.region <- (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) / 2) # in y coordinates, to position axis labeling at the top of the graph (according to y scale). Ex mid.top.space
        y.top.fig.region <- (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4])) # in y coordinates
        y.top.plot.region <- par("usr")[4] # in y coordinates, top of the plot region (according to y scale)
        y.bottom.plot.region <- par("usr")[3] # in y coordinates, bottom of the plot region (according to y scale)
        y.mid.plot.region <- ((par("usr")[3] + par("usr")[4]) / 2) # in x coordinates, right of the plot region (according to x scale)
    }
    if(x.side == 1 | x.side == 3){
        par(xpd=FALSE, xaxt="s")
        if(is.null(x.categ) & x.log.scale == TRUE){
            if(any(par()$xaxp[1:2] == 0)){
                if(par()$xaxp[1] == 0){
                    par(xaxp = c(10^-30, par()$xaxp[2:3])) # because log10(par()$xaxp[1] == 0) == -Inf
                }
                if(par()$xaxp[2] == 0){
                    par(xaxp = c(par()$xaxp[1], 10^-30, par()$xaxp[3])) # because log10(par()$xaxp[2] == 0) == -Inf
                }
            }
            axis(side=x.side, at=c(10^par()$usr[1], 10^par()$usr[2]), labels=rep("", 2), lwd=1, lwd.ticks=0) # draw the axis line
            mtext(side = x.side, text = x.lab, line = x.dist.legend / 0.2, las = 0, cex = x.label.magnific)
            par(tcl = -par()$mgp[2] * sec.tick.length) # length of the secondary ticks are reduced
            suppressWarnings(rug(10^outer(c((log10(par("xaxp")[1]) -1):log10(par("xaxp")[2])), log10(1:10), "+"), ticksize = NA, side = x.side)) # ticksize = NA to allow the use of par()$tcl value
            par(tcl = -par()$mgp[2] * tick.length) # back to main ticks
            axis(side = x.side, at = c(1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10), labels = expression(10^-15, 10^-14, 10^-13, 10^-12, 10^-11, 10^-10, 10^-9, 10^-8, 10^-7, 10^-6, 10^-5, 10^-4, 10^-3, 10^-2, 10^-1, 10^0, 10^1, 10^2, 10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9, 10^10), lwd = 0, lwd.ticks = 1, cex.axis = x.axis.magnific)
            x.text <- 10^par("usr")[2]
        }else if(is.null(x.categ) & x.log.scale == FALSE){
            axis(side=x.side, at=c(par()$usr[1], par()$usr[2]), labels=rep("", 2), lwd=1, lwd.ticks=0) # draw the axis line
            axis(side=x.side, at=round(seq(par()$xaxp[1], par()$xaxp[2], length.out=par()$xaxp[3]+1), 2), cex.axis = x.axis.magnific) # axis(side=x.side, at=round(seq(par()$xaxp[1], par()$xaxp[2], length.out=par()$xaxp[3]+1), 2), labels = format(round(seq(par()$xaxp[1], par()$xaxp[2], length.out=par()$xaxp[3]+1), 2), big.mark=','), cex.axis = x.axis.magnific) # to get the 1000 comma separator
            mtext(side = x.side, text = x.lab, line = x.dist.legend / 0.2, las = 0, cex = x.label.magnific)
            if(x.nb.inter.tick > 0){
                inter.tick.unit <- (par("xaxp")[2] - par("xaxp")[1]) / par("xaxp")[3]
                par(tcl = -par()$mgp[2] * sec.tick.length) # length of the ticks are reduced
                suppressWarnings(rug(seq(par("xaxp")[1] - 10 * inter.tick.unit, par("xaxp")[2] + 10 * inter.tick.unit, by = inter.tick.unit / (1 + x.nb.inter.tick)), ticksize = NA, x.side)) # ticksize = NA to allow the use of par()$tcl value
                par(tcl = -par()$mgp[2] * tick.length) # back to main ticks
            }
            x.text <- par("usr")[2]
        }else if(( ! is.null(x.categ)) & x.log.scale == FALSE){
            if(is.null(x.categ.pos)){
                x.categ.pos <- 1:length(x.categ)
            }else if(length(x.categ.pos) != length(x.categ)){
                stop("\n\nPROBLEM: x.categ.pos MUST BE THE SAME LENGTH AS x.categ\n\n")
            }
            par(xpd = TRUE)
            if(x.side == 1){
                segments(x0 = x.left.plot.region, x1 = x.right.plot.region, y0 = y.bottom.plot.region, y1 = y.bottom.plot.region) # draw the line of the axis
                text(x = x.categ.pos, y = y.mid.bottom.fig.region, labels = x.categ, srt = text.angle, cex = x.axis.magnific)
            }else if(x.side == 3){
                segments(x0 = x.left.plot.region, x1 = x.right.plot.region, y0 = y.top.plot.region, y1 = y.top.plot.region) # draw the line of the axis
                text(x = x.categ.pos, y = y.mid.top.fig.region, labels = x.categ, srt = text.angle, cex = x.axis.magnific)
            }else{
                stop("\n\nARGUMENT x.side CAN ONLY BE 1 OR 3\n\n")
            }
            par(xpd = FALSE)
            x.text <- par("usr")[2]
        }else{
            stop("\n\nPROBLEM WITH THE x.side (", x.side ,") OR x.log.scale (", x.log.scale,") ARGUMENTS\n\n")
        }
    }else{
        x.text <- par("usr")[2]
    }
    if(y.side == 2 | y.side == 4){
        par(xpd=FALSE, yaxt="s")
        if(is.null(y.categ) & y.log.scale == TRUE){
            if(any(par()$yaxp[1:2] == 0)){
                if(par()$yaxp[1] == 0){
                    par(yaxp = c(10^-30, par()$yaxp[2:3])) # because log10(par()$yaxp[1] == 0) == -Inf
                }
                if(par()$yaxp[2] == 0){
                    par(yaxp = c(par()$yaxp[1], 10^-30, par()$yaxp[3])) # because log10(par()$yaxp[2] == 0) == -Inf
                }
            }
            axis(side=y.side, at=c(10^par()$usr[3], 10^par()$usr[4]), labels=rep("", 2), lwd=1, lwd.ticks=0) # draw the axis line
            par(tcl = -par()$mgp[2] * sec.tick.length) # length of the ticks are reduced
            suppressWarnings(rug(10^outer(c((log10(par("yaxp")[1])-1):log10(par("yaxp")[2])), log10(1:10), "+"), ticksize = NA, side = y.side)) # ticksize = NA to allow the use of par()$tcl value
            par(tcl = -par()$mgp[2] * tick.length) # back to main tick length
            axis(side = y.side, at = c(1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10), labels = expression(10^-15, 10^-14, 10^-13, 10^-12, 10^-11, 10^-10, 10^-9, 10^-8, 10^-7, 10^-6, 10^-5, 10^-4, 10^-3, 10^-2, 10^-1, 10^0, 10^1, 10^2, 10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9, 10^10), lwd = 0, lwd.ticks = 1, cex.axis = y.axis.magnific)
            y.text <- 10^(par("usr")[4] + (par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3]) * (1 - par("plt")[4]))
            mtext(side = y.side, text = y.lab, line = y.dist.legend / 0.2, las = 0, cex = y.label.magnific)
        }else if(is.null(y.categ) & y.log.scale == FALSE){
            axis(side=y.side, at=c(par()$usr[3], par()$usr[4]), labels=rep("", 2), lwd=1, lwd.ticks=0) # draw the axis line
            axis(side=y.side, at=round(seq(par()$yaxp[1], par()$yaxp[2], length.out=par()$yaxp[3]+1), 2), cex.axis = y.axis.magnific)
            mtext(side = y.side, text = y.lab, line = y.dist.legend / 0.2, las = 0, cex = y.label.magnific)
            if(y.nb.inter.tick > 0){
                inter.tick.unit <- (par("yaxp")[2] - par("yaxp")[1]) / par("yaxp")[3]
                par(tcl = -par()$mgp[2] * sec.tick.length) # length of the ticks are reduced
                suppressWarnings(rug(seq(par("yaxp")[1] - 10 * inter.tick.unit, par("yaxp")[2] + 10 * inter.tick.unit, by =  inter.tick.unit / (1 + y.nb.inter.tick)), ticksize = NA, side=y.side)) # ticksize = NA to allow the use of par()$tcl value
                par(tcl = -par()$mgp[2] * tick.length) # back to main tick length
            }
            y.text <- (par("usr")[4] + (par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3]) * (1 - par("plt")[4]))
        }else if(( ! is.null(y.categ)) & y.log.scale == FALSE){
            if(is.null(y.categ.pos)){
                y.categ.pos <- 1:length(y.categ)
            }else if(length(y.categ.pos) != length(y.categ)){
                stop("\n\nPROBLEM: y.categ.pos MUST BE THE SAME LENGTH AS y.categ\n\n")
            }
            axis(side = y.side, at = y.categ.pos, labels = rep("", length(y.categ)), lwd=0, lwd.ticks=1) # draw the line of the axis
            par(xpd = TRUE)
            if(y.side == 2){
                text(x = x.mid.left.fig.region, y = y.categ.pos, labels = y.categ, srt = text.angle, cex = y.axis.magnific)
            }else if(y.side == 4){
                text(x = x.mid.right.fig.region, y = y.categ.pos, labels = y.categ, srt = text.angle, cex = y.axis.magnific)
            }else{
                stop("\n\nARGUMENT y.side CAN ONLY BE 2 OR 4\n\n")
            }
            par(xpd = FALSE)
            y.text <- (par("usr")[4] + (par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3]) * (1 - par("plt")[4]))
        }else{
            stop("\n\nPROBLEM WITH THE y.side (", y.side ,") OR y.log.scale (", y.log.scale,") ARGUMENTS\n\n")
        }
    }else{
        y.text <- (par("usr")[4] + (par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3]) * (1 - par("plt")[4]))
    }
    par(xpd=NA)
    text(x = x.mid.right.fig.region, y = y.text, corner.text, adj=c(1, 1.1), cex = magnific.corner.text) # text at the topright corner. Replace x.right.fig.region by x.text if text at the right edge of the plot region
    if(just.label.add == TRUE & x.side == 0 & x.lab != ""){
        text(x = x.mid.plot.region, y = y.mid.bottom.fig.region, x.lab, adj=c(0.5, 0.5), cex = x.label.magnific) # x label
    }
    if(just.label.add == TRUE & y.side == 0 & y.lab != ""){
        text(x = y.mid.plot.region, y = x.mid.left.fig.region, y.lab, adj=c(0.5, 0.5), cex = y.label.magnific) # x label
    }
    par(xpd=FALSE)
    if(par.reset == TRUE){
        tempo.par <- fun_open_window(pdf.disp = FALSE, return.output = TRUE)
        invisible(dev.off()) # close the new window
        if( ! is.null(custom.par)){
            if( ! names(custom.par) %in% names(tempo.par$ini.par)){
                tempo.cat <- paste0("\n\n================\n\nERROR IN fun_feature_post_plot(): custom.par ARGUMENT SHOULD HAVE THE NAMES OF THE COMPARTMENT LIST COMING FROM THE par() LIST\n\n================\n\n")
                stop(tempo.cat)
            }
            par(custom.par)
            text <- c(text, "\nGRAPH PARAMETERS SET TO VALUES DEFINED BY custom.par ARGUMENT\n")
        }else{
            par(tempo.par$ini.par)
            text <- c(text, "\nGRAPH PARAMETERS RESET TO par() DEFAULT VALUES\n")
        }
    }
    output <- list(x.mid.left.dev.region = x.mid.left.dev.region, x.left.dev.region = x.left.dev.region, x.mid.right.dev.region = x.mid.right.dev.region, x.right.dev.region = x.right.dev.region, x.mid.left.fig.region = x.mid.left.fig.region, x.left.fig.region = x.left.fig.region, x.mid.right.fig.region = x.mid.right.fig.region, x.right.fig.region = x.right.fig.region, x.left.plot.region = x.left.plot.region, x.right.plot.region = x.right.plot.region, x.mid.plot.region = x.mid.plot.region, y.mid.bottom.dev.region = y.mid.bottom.dev.region, y.bottom.dev.region = y.bottom.dev.region, y.mid.top.dev.region = y.mid.top.dev.region, y.top.dev.region = y.top.dev.region, y.mid.bottom.fig.region = y.mid.bottom.fig.region, y.bottom.fig.region = y.bottom.fig.region, y.mid.top.fig.region = y.mid.top.fig.region, y.top.fig.region = y.top.fig.region, y.top.plot.region = y.top.plot.region, y.bottom.plot.region = y.bottom.plot.region, y.mid.plot.region = y.mid.plot.region, text = text)
    return(output)
Gael  MILLOT's avatar
Gael MILLOT committed
2421
2422
2423
2424
2425
2426
2427
2428
}


######## fun_close_specif_window() #### Closing specific graphic windows


# Check OK: clear to go Apollo
fun_close_specif_window <- function(kind = "pdf", return.text = FALSE){
Gael  MILLOT's avatar
Gael MILLOT committed
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
    # AIM:
    # close only specific graphic windows (devices)
    # REQUIRED FUNCTIONS
    # fun_param_check()
    # ARGUMENTS:
    # kind: vector, among c("windows", "quartz", "x11", "X11", "pdf", "bmp", "png", "tiff"), indicating the kind of graphic windows (devices) to close. BEWARE: either "windows", "quartz", "x11" or "X11" means that all the X11 GUI graphics devices will be closed, whatever the OS used
    # return.text: print text regarding the kind parameter and the devices that were finally closed?
    # RETURN
    # text regarding the kind parameter and the devices that were finally closed
    # EXAMPLES
    # windows() ; windows() ; pdf() ; dev.list() ; fun_close_specif_window(kind = c("pdf", "x11"), return.text = TRUE) ; dev.list()
    # DEBUGGING
    # kind = c("windows", "pdf") ; return.text = FALSE # for function debugging
    # required function checking
    if(length(find("fun_param_check", mode = "function")) == 0){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_close_specif_window(): REQUIRED fun_param_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
        stop(tempo.cat)
    }
    # end required function checking
    # argument checking
    arg.check <- NULL # for function debbuging
    checked.arg.names <- NULL # for function debbuging
    ee <- expression(arg.check <- c(arg.check, tempo$problem) , checked.arg.names <- c(checked.arg.names, tempo$param.name))
    tempo <- fun_param_check(data = kind, options = c("windows", "quartz", "x11", "X11", "pdf", "bmp", "png", "tiff")) ; eval(ee)
    tempo <- fun_param_check(data = return.text, class = "logical", length = 1) ; eval(ee)
    if(any(arg.check) == TRUE){
        stop() # nothing else because print = TRUE by default in fun_param_check()
    }
    # 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_param_check_dev)) # activate this line and use the function to check arguments status and if they have been checked using fun_param_check()
    # end argument checking
    text <- paste0("THE REQUIRED KIND OF GRAPHIC DEVICES TO CLOSE ARE ", paste(kind, collapse = " "))
    if(Sys.info()["sysname"] == "Windows"){ # Note that .Platform$OS.type() only says "unix" for macOS and Linux and "Windows" for Windows
        if(any(kind %in% c("windows", "quartz", "x11", "X11"))){
            tempo <- kind %in% c("windows", "quartz", "x11", "X11")
            kind[tempo] <- "windows" #  # term are replaced by what is displayed when using a <- dev.list() ; names(a)
        }
    }else if(Sys.info()["sysname"] == "Linux"){
        if(any(kind %in% c("windows", "quartz", "x11", "X11"))){
            tempo.device <- suppressWarnings(try(X11(), silent = TRUE))[] # open a X11 window to try to recover the X11 system used
            if( ! is.null(tempo.device)){
                text <- paste0(text, "\nCANNOT CLOSE GUI GRAPHIC DEVICES AS REQUIRED BECAUSE THIS LINUX SYSTEM DOES NOT HAVE IT")
            }else{
                tempo <- kind %in% c("windows", "quartz", "x11", "X11")
                kind[tempo] <- names(dev.list()[length(dev.list())]) #  # term are replaced by what is displayed when using a <- dev.list() ; names(a)
                invisible(dev.off()) # close the X11 opened by tempo
            }
        }
    }else{ # for macOS
        if(any(kind %in% c("windows", "quartz", "x11", "X11"))){
            tempo <- kind %in% c("windows", "quartz", "x11", "X11")
            kind[tempo] <- "quartz" # term are replaced by what is displayed when using a <- dev.list() ; names(a)
        }
    }
    kind <- unique(kind)
    if(length(dev.list()) != 0){
        for(i in length(names(dev.list())):1){
            if(names(dev.list())[i] %in% kind){
                text <- paste0(text, "\n", names(dev.list())[i], " DEVICE NUMBER ", dev.list()[i], " HAS BEEN CLOSED")
                invisible(dev.off(dev.list()[i]))
            }
        }
    }
    if(return.text == TRUE){
        return(text)
    }
Gael  MILLOT's avatar
Gael MILLOT committed
2494
2495
2496
}


Gael  MILLOT's avatar
Gael MILLOT committed
2497
2498
2499
######## fun_var_trim_display() #### Display values from a quantitative variable and trim according to defined cut-offs


Gael  MILLOT's avatar
Gael MILLOT committed
2500
fun_var_trim_display <- function(data, displayed.nb = NULL, single.value.display = FALSE, trim.method = "", trim.cutoffs = c(0.05, 0.975), interval.scale.disp = TRUE, down.space = 0.75, left.space = 0.75, up.space = 0.3, right.space = 0.25, orient = 1, dist.legend = 0.37, box.type = "l", amplif.label = 1.25, amplif.axis = 1.25, std.x.range = TRUE, std.y.range = TRUE, cex.pt = 0.2, col.box = hsv(0.55, 0.8, 0.8), x.nb.inter.tick = 4, y.nb.inter.tick = 0, tick.length = 1, sec.tick.length = 0.75, corner.text = "", amplif.legend = 1, magnific.corner.text = 0.75, trim.return = FALSE){
Gael  MILLOT's avatar
Gael MILLOT committed
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
    # AIM:
    # trim and display values from a numeric vector or matrix
    # plot 4 graphs: stripchart of values, stripchart of rank of values, hitogramme and normal QQPlot
    # different kinds of intervals are displayed on the top of graphes to facilitate the analysis of the variable and a trimming setting
    # the trimming interval chosen is displayed on top of graphs
    # both trimmed and not trimmed values are returned in a list
    # REQUIRED FUNCTIONS
    # fun_param_check()
    # ARGUMENTS
    # data: values to plot (either a numeric vector or a numeric matrix)
    # displayed.nb: number of values displayed. If NULL, all the values are displayed. Otherwise, if the number of values is over displayed.nb, then displayed.nb values are displayed after random selection
    # single.value.display: provide the 4 graphs if data is made of a single  (potentially repeated value)? If FALSE, an empty graph is displayed if data is made of a single (potentially repeated value). And the return list is made of NULL compartments
    # trim.method: Write "" if not required. write "mean.sd" if mean +/- sd has to be displayed as a trimming interval (only recommanded for normal distribution). Write "quantile" to display a trimming interval based on quantile cut-offs. No other possibility allowed. See trim.cutoffs below
    # trim.cutoffs: 2 values cutoff for the trimming interval displayed, each value between 0 and 1. Not used if trim.method == "".The couple of values c(lower, upper) represents the lower and upper boundaries of the trimming interval (in proportion), which represent the interval of distribution kept (between 0 and 1). Example: trim.cutoffs = c(0.05, 0.975). What is strictly kept for the display is ]lower , upper[, boundaries excluded. Using the "mean.sd" method, 0.025 and 0.975 represent 95% CI which is mean +/- 1.96 * sd
    # interval.scale.disp: display sd and quantiles intervals on top of graphs ?
    # down.space: lower vertical margin (in inches, mai argument of par())
    # left.space: left horizontal margin (in inches, mai argument of par())
    # up.space: upper vertical margin between plot region and grapical window (in inches, mai argument of par())
    # right.space: right horizontal margin (in inches, mai argument of par())
    # orient: scale number orientation (las argument of par()). 0, always parallel to the axis; 1, always horizontal; 2, always perpendicular to the axis; 3, always vertical
    # dist.legend: numeric value that moves axis legends away in inches (first number of mgp argument of par() but in inches thus / 0.2)
    # box.type: bty argument of par(). Either "o", "l", "7", "c", "u", "]", the resulting box resembles the corresponding upper case letter. A value of "n" suppresses the box
    # amplif.label: increase or decrease the size of the text in legends
    # amplif.axis: increase or decrease the size of the scale numbers in axis
    # std.x.range: standard range on the x-axis? TRUE (no range extend) or FALSE (4% range extend). Controls xaxs argument of par() (TRUE is xaxs = "i", FALSE is xaxs = "r")
    # std.y.range: standard range on the y-axis? TRUE (no range extend) or FALSE (4% range extend). Controls yaxs argument of par() (TRUE is yaxs = "i", FALSE is yaxs = "r")
    # cex.pt: size of points in stripcharts (in inches, thus cex.pt will be thereafter / 0.2)
    # col.box: color of boxplot
    # x.nb.inter.tick: number of secondary ticks between main ticks on x-axis (only if not log scale). Zero means non secondary ticks
    # y.nb.inter.tick: number of secondary ticks between main ticks on y-axis (only if not log scale). Zero means non secondary ticks
    # tick.length: length of the ticks (1 means complete the distance between the plot region and the axis numbers, 0.5 means half the length, etc. 0 means no tick
    # sec.tick.length: length of the secondary ticks (1 means complete the distance between the plot region and the axis numbers, 0.5 means half the length, etc., 0 for no ticks)
    # corner.text: text to add at the top right corner of the window
    # amplif.legend: increase or decrease the size of the text of legend
    # magnific.corner.text: increase or decrease the size of the text
    # trim.return: return the trimmed and non trimmed values? NULL returned for trimmed and non trimmed values if trim.method == ""
    # RETURN
    # a list containing:
    # $trim.method: correspond to trim.method above
    # $trim.cutoffs: correspond to trim.cutoffs above
    # $real.trim.cutoffs: the two boundary values (in the unit of the numeric vector or numeric matrix analyzed). NULL 
    # $trimmed.values: the values outside of the trimming interval as defined in trim.cutoffs above
    # $kept.values: the values inside the trimming interval as defined in trim.cutoffs above
    # EXAMPLES
    # fun_var_trim_display(data = c(1:100, 1:10), displayed.nb = NULL, single.value.display = FALSE, trim.method = "mean.sd", trim.cutoffs = c(0.05, 0.975), interval.scale.disp = TRUE, down.space = 0.75, left.space = 0.75, up.space = 0.3, right.space = 0.25, orient = 1, dist.legend = 0.37, box.type = "l", amplif.label = 1.25, amplif.axis = 1.25, std.x.range = TRUE, std.y.range = TRUE, cex.pt = 0.2, col.box = hsv(0.55, 0.8, 0.8), x.nb.inter.tick = 4, y.nb.inter.tick = 0, tick.length = 0.5, sec.tick.length = 0.3, corner.text = "", amplif.legend = 1, magnific.corner.text = 0.75, trim.return = TRUE)
    # DEBUGGING
    # data = c(1:100, 1:10) ; displayed.nb = NULL ; single.value.display = FALSE ; trim.method = "quantile" ; trim.cutoffs = c(0.05, 0.975) ; interval.scale.disp = TRUE ; down.space = 1 ; left.space = 1 ; up.space = 0.5 ; right.space = 0.25 ; orient = 1 ; dist.legend = 0.5 ; box.type = "l" ; amplif.label = 1 ; amplif.axis = 1 ; std.x.range = TRUE ; std.y.range = TRUE ; cex.pt = 0.1 ; col.box = hsv(0.55, 0.8, 0.8) ; x.nb.inter.tick = 4 ; y.nb.inter.tick = 0 ; tick.length = 0.5 ; sec.tick.length = 0.3 ; corner.text = "" ; amplif.legend = 1 ; magnific.corner.text = 0.75 ; trim.return = TRUE # for function debugging
    # required function checking
    if(length(find("fun_param_check", mode = "function")) == 0){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_var_trim_display(): REQUIRED fun_param_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
        stop(tempo.cat)
    }
    # end required function checking
    # argument checking
    arg.check <- NULL # for function debbuging
    checked.arg.names <- NULL # for function debbuging
    ee <- expression(arg.check <- c(arg.check, tempo$problem) , checked.arg.names <- c(checked.arg.names, tempo$param.name))
    if( ! (all(class(data) == "numeric") | all(class(data) == "integer") | (all(class(data) == "matrix") & mode(data) == "numeric"))){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_var_trim_display(): data ARGUMENT MUST BE A NUMERIC VECTOR OR NUMERIC MATRIX\n\n================\n\n")
        stop(tempo.cat)
    }
    if( ! is.null(displayed.nb)){
Gael  MILLOT's avatar
Gael MILLOT committed
2563
        tempo <- fun_param_check(data = displayed.nb, mode = "numeric", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2564
2565
2566
2567
2568
2569
2570
        if(displayed.nb < 2){
            tempo.cat <- paste0("\n\n================\n\nERROR IN fun_var_trim_display(): displayed.nb ARGUMENT MUST BE A SINGLE INTEGER VALUE GREATER THAN 1 AND NOT: ", paste(displayed.nb, collapse = " "), "\n\n================\n\n")
            stop(tempo.cat)
        }
    }
    tempo <- fun_param_check(data = single.value.display, class = "logical", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = trim.method, options = c("", "mean.sd", "quantile"), length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2571
    tempo <- fun_param_check(data = trim.cutoffs, mode = "numeric", length = 2, prop = TRUE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2572
    tempo <- fun_param_check(data = interval.scale.disp, class = "logical", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2573
2574
2575
2576
2577
2578
    tempo <- fun_param_check(data = down.space, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = left.space, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = up.space, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = right.space, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = orient, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = dist.legend, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2579
    tempo <- fun_param_check(data = box.type, options = c("o", "l", "7", "c", "u", "]", "n"), length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2580
2581
    tempo <- fun_param_check(data = amplif.label, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = amplif.axis, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2582
2583
    tempo <- fun_param_check(data = std.x.range, class = "logical", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = std.y.range, class = "logical", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2584
    tempo <- fun_param_check(data = cex.pt, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2585
2586
2587
    tempo <- fun_param_check(data = col.box, class = "character", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = x.nb.inter.tick, class = "integer", length = 1, neg.values = FALSE, double.as.integer.allowed = TRUE) ; eval(ee)
    tempo <- fun_param_check(data = y.nb.inter.tick, class = "integer", length = 1, neg.values = FALSE, double.as.integer.allowed = TRUE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2588
2589
    tempo <- fun_param_check(data = tick.length, mode = "numeric", length = 1, prop = TRUE) ; eval(ee)
    tempo <- fun_param_check(data = sec.tick.length, mode = "numeric", length = 1, prop = TRUE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2590
    tempo <- fun_param_check(data = corner.text, class = "character", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2591
2592
    tempo <- fun_param_check(data = amplif.legend, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
    tempo <- fun_param_check(data = magnific.corner.text, mode = "numeric", length = 1, neg.values = FALSE) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
    tempo <- fun_param_check(data = trim.return, class = "logical", length = 1) ; eval(ee)
    if(any(arg.check) == TRUE){
        stop() # nothing else because print = TRUE by default in fun_param_check()
    }
    # 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_param_check_dev)) # activate this line and use the function to check arguments status and if they have been checked using fun_param_check()
    # end argument checking
    if(class(data) == "matrix"){
        data <- as.vector(data)
    }
    color.cut <- hsv(0.75, 1, 1)  # color of interval selected
    col.mean <- hsv(0.25, 1, 0.8) # color of interval using mean+/-sd
    col.quantile <- "orange" # color of interval using quantiles
    quantiles.selection <- c(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.975, 0.99) # quantiles used in axis to help for choosing trimming cutoffs
    if(single.value.display == FALSE & length(unique(data)) == 1){
        par(bty = "n", xaxt = "n", yaxt = "n", xpd = TRUE)
        plot(1, pch = 16, col = "white", xlab = "", ylab = "")
        text(x = 1, y = 1, paste0("No graphic displayed\nBecause data made of a single different value (", formatC(as.double(table(data))), ")"), cex = 2)
        output <- list(trim.method = NULL, trim.cutoffs = NULL, real.trim.cutoffs = NULL, trimmed.values = NULL, kept.values = NULL)
    }else{
        output <- list(trim.method = trim.method, trim.cutoffs = trim.cutoffs, real.trim.cutoffs = NULL, trimmed.values = NULL, kept.values = NULL)
        fun.rug <- function(sec.tick.length.f = sec.tick.length, x.nb.inter.tick.f = x.nb.inter.tick, y.nb.inter.tick.f = y.nb.inter.tick){
            if(x.nb.inter.tick.f > 0){
                inter.tick.unit <- (par("xaxp")[2] - par("xaxp")[1]) / par("xaxp")[3]
                par.ini <- par()[c("xpd", "tcl")]
                par(xpd = FALSE)
                par(tcl = -par()$mgp[2] * sec.tick.length.f) # tcl gives the length of the ticks as proportion of line text, knowing that mgp is in text lines. So the main ticks are a 0.5 of the distance of the axis numbers by default. The sign provides the side of the tick (negative for outside of the plot region)
                suppressWarnings(rug(seq(par("xaxp")[1] - 10 * inter.tick.unit, par("xaxp")[2] + 10 * inter.tick.unit, by = inter.tick.unit / (1 + x.nb.inter.tick.f)), ticksize = NA, side = 1)) # ticksize = NA to allow the use of par()$tcl value
                par(par.ini)
                rm(par.ini)
            }
            if(y.nb.inter.tick.f > 0){
                inter.tick.unit <- (par("yaxp")[2] - par("yaxp")[1]) / par("yaxp")[3]
                par.ini <- par()[c("xpd", "tcl")]
                par(xpd = FALSE)
                par(tcl = -par()$mgp[2] * sec.tick.length.f) # tcl gives the length of the ticks as proportion of line text, knowing that mgp is in text lines. So the main ticks are a 0.5 of the distance of the axis numbers by default. The sign provides the side of the tick (negative for outside of the plot region)
                suppressWarnings(rug(seq(par("yaxp")[1] - 10 * inter.tick.unit, par("yaxp")[2] + 10 * inter.tick.unit, by = inter.tick.unit / (1 + y.nb.inter.tick.f)), ticksize = NA, side = 2)) # ticksize = NA to allow the use of par()$tcl value
                par(par.ini)
                rm(par.ini)
            }
        }
        fun.add.cut <- function(data.f, trim.method.f = trim.method, trim.cutoffs.f = trim.cutoffs, color.cut.f = color.cut, return.f = FALSE){
            # DEBUGGING
            # data.f = data ; trim.method.f = "mean.sd"; trim.cutoffs.f = trim.cutoffs ; color.cut.f = color.cut ; return.f = TRUE
            real.trim.cutoffs.f <- NULL
            if(trim.method.f != ""){
                data.f <- sort(data.f)
                par.ini <- par()$xpd
                par(xpd = FALSE)
                if(trim.method.f == "mean.sd"){
                    real.trim.cutoffs.f <- qnorm(trim.cutoffs.f, mean(data.f, na.rm = TRUE), sd(data.f, na.rm = TRUE))
                    abline(v = qnorm(trim.cutoffs.f, mean(data.f, na.rm = TRUE), sd(data.f, na.rm = TRUE)), col = color.cut.f)
                    segments(qnorm(trim.cutoffs.f[1], mean(data.f, na.rm = TRUE), sd(data.f, na.rm = TRUE)), par()$usr[4] * 0.75, qnorm(trim.cutoffs.f[2], mean(data.f, na.rm = TRUE), sd(data.f, na.rm = TRUE)), par()$usr[4] * 0.75, col = color.cut.f)
                }
                if(trim.method.f == "quantile"){
                    real.trim.cutoffs.f <- quantile(data.f, probs = trim.cutoffs.f, type = 7)
                    abline(v = quantile(data.f, probs = trim.cutoffs.f, type = 7), col = color.cut.f)
                    segments(quantile(data.f, probs = trim.cutoffs.f[1], type = 7), par()$usr[4] * 0.75, quantile(data.f, probs = trim.cutoffs.f[2], type = 7), par()$usr[4] * 0.75, col = color.cut.f)
                }
                par(par.ini)
                if(return.f == TRUE){
                    trimmed.values.f <- data.f[data.f <= real.trim.cutoffs.f[1] | data.f >= real.trim.cutoffs.f[2]]
                    kept.values.f <- data.f[data.f > real.trim.cutoffs.f[1] & data.f < real.trim.cutoffs.f[2]]
                }
            }else{
                real.trim.cutoffs.f <- NULL
                trimmed.values.f <- NULL
                kept.values.f <- NULL
            }
            if(return.f == TRUE){
                output <- list(trim.method = trim.method.f, trim.cutoffs = trim.cutoffs.f, real.trim.cutoffs = real.trim.cutoffs.f, trimmed.values = trimmed.values.f, kept.values = kept.values.f)
                return(output)
            }
        }
        fun.interval.scale.display <- function(data.f, col.quantile.f = col.quantile, quantiles.selection.f = quantiles.selection, col.mean.f = col.mean){ # intervals on top of graphs
            par.ini <- par()[c("mgp", "xpd")]
            par(mgp = c(0.25, 0.25, 0), xpd = NA)
            axis(side = 3, at = c(par()$usr[1], par()$usr[2]), labels = rep("", 2), col = col.quantile.f, lwd.ticks = 0)
            par(xpd = FALSE)
            axis(side = 3, at = quantile(as.vector(data.f), probs = quantiles.selection.f, type = 7), labels = quantiles.selection.f, col.axis = col.quantile.f, col = col.quantile.f)
            par(mgp = c(1.75, 1.75, 1.5), xpd = NA)
            axis(side = 3, at = c(par()$usr[1], par()$usr[2]), labels = rep("", 2), col = col.mean.f, lwd.ticks = 0)
            par(xpd = FALSE)
            axis(side = 3, at = m + s * qnorm(quantiles.selection.f), labels = formatC(round(qnorm(quantiles.selection.f), 2)), col.axis = col.mean.f, col = col.mean.f, lwd.ticks = 1)
            par(par.ini)
        }
        zone<-matrix(1:4, ncol=2)
        layout(zone)
        par(omi = c(0, 0, 1.5, 0), mai = c(down.space, left.space, up.space, right.space), las = orient, mgp = c(dist.legend / 0.2, 0.5, 0), xpd = FALSE, bty= box.type, cex.lab = amplif.label, cex.axis = amplif.axis, xaxs = ifelse(std.x.range, "i", "r"), yaxs = ifelse(std.y.range, "i", "r"))
        par(tcl = -par()$mgp[2] * tick.length) # tcl gives the length of the ticks as proportion of line text, knowing that mgp is in text lines. So the main ticks are a 0.5 of the distance of the axis numbers by default. The sign provides the side of the tick (negative for outside of the plot region)
        if(is.null(displayed.nb)){
            sampled.data <- as.vector(data)
            if(corner.text == ""){
                corner.text <- paste0("ALL VALUES OF THE DATASET DISPLAYED")
            }else{
                corner.text <- paste0(corner.text, "\nALL VALUES OF THE DATASET DISPLAYED")
            }
        }else{
            if(length(as.vector(data)) > displayed.nb){
                sampled.data <- sample(as.vector(data), displayed.nb, replace = FALSE)
                if(corner.text == ""){
                    corner.text <- paste0("BEWARE: ONLY ", displayed.nb, " VALUES ARE DISPLAYED AMONG THE ", length(as.vector(data)), " VALUES OF THE DATASET ANALYZED")
                }else{
                    corner.text <- paste0(corner.text, "\nBEWARE: ONLY ", displayed.nb, " VALUES ARE DISPLAYED AMONG THE ", length(as.vector(data)), " VALUES OF THE DATASET ANALYZED")
                }
            }else{
                sampled.data <- as.vector(data)
                if(corner.text == ""){
                    corner.text <- paste0("BEWARE: THE DISPLAYED NUMBER OF VALUES PARAMETER ", deparse(substitute(displayed.nb)), " HAS BEEN SET TO ", displayed.nb, " WHICH IS ABOVE THE NUMBER OF VALUES OF THE DATASET ANALYZED -> ALL VALUES DISPLAYED")
                }else{
                    corner.text <- paste0(corner.text, "\nBEWARE: THE DISPLAYED NUMBER OF VALUES PARAMETER ", deparse(substitute(displayed.nb)), " HAS BEEN SET TO ", displayed.nb, " WHICH IS ABOVE THE NUMBER OF VALUES OF THE DATASET ANALYZED -> ALL VALUES DISPLAYED")
                }
            }
        }
        stripchart(sampled.data, method="jitter", jitter=0.4, vertical=FALSE, ylim=c(0.5, 1.5), group.names = "", xlab = "Value", ylab="", pch=1, cex = cex.pt / 0.2)
        fun.rug(y.nb.inter.tick.f = 0)
        boxplot(as.vector(data), horizontal=TRUE, add=TRUE, boxwex = 0.4, staplecol = col.box, whiskcol = col.box, medcol = col.box, boxcol = col.box, range = 0, whisklty = 1)
        m <- mean(as.vector(data), na.rm = TRUE)
        s <- sd(as.vector(data), na.rm = TRUE)
        segments(m, 0.8, m, 1, lwd=2, col="red") # mean 
        segments(m -1.96 * s, 0.9, m + 1.96 * s, 0.9, lwd=1, col="red") # mean 
        graph.xlim <- par()$usr[1:2] # for hist() and qqnorm() below
        if(interval.scale.disp == TRUE){
            fun.interval.scale.display(data.f = data)
            if(corner.text == ""){
                corner.text <-  paste0("MULTIPLYING FACTOR DISPLAYED (MEAN +/- SD) ON SCALES: ", paste(formatC(round(qnorm(quantiles.selection), 2))[-(1:(length(quantiles.selection) - 1) / 2)], collapse = ", "), "\nQUANTILES DISPLAYED ON SCALES: ", paste(quantiles.selection, collapse = ", "))
            }else{
                corner.text <-  paste0(corner.text, "\nMULTIPLYING FACTOR DISPLAYED (MEAN +/- SD) ON SCALES: ", paste(formatC(round(qnorm(quantiles.selection), 2))[-(1:(length(quantiles.selection) - 1) / 2)], collapse = ", "), "\nQUANTILES DISPLAYED ON SCALES: ", paste(quantiles.selection, collapse = ", "))
            }
        }
        output.tempo <- fun.add.cut(data.f = data, return.f = TRUE) # to recover real.trim.cutoffs
        if(trim.return == TRUE){
            output <- output.tempo
        }
        par(xpd = NA)
        if(trim.method != ""){
            if(corner.text == ""){
                corner.text <-  paste0("SELECTED CUT-OFFS (PROPORTION): ", paste(trim.cutoffs, collapse = ", "), "\nSELECTED CUT-OFFS: ", paste(output.tempo$real.trim.cutoffs, collapse = ", "))
            }else{
                corner.text <-  paste0(corner.text, "\nSELECTED CUT-OFFS (PROPORTION): ", paste(trim.cutoffs, collapse = ", "), "\nSELECTED CUT-OFFS: ", paste(output.tempo$real.trim.cutoffs, collapse = ", "))
            }
            if(interval.scale.disp == TRUE){
                legend(x = (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("omd")[2] - par("omd")[1])) * par("omd")[1]), y = (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / (par("omd")[4] - par("omd")[3])) * (1 - par("omd")[4]) / 2), legend = c(c("min, Q1, Median, Q3, max"), "mean +/- 1.96sd", paste0("Trimming interval: ", paste0(trim.cutoffs, collapse = " , ")), "Mean +/- sd multiplying factor", "Quantile"), yjust = 0, lty=1, col=c(col.box, "red", color.cut, col.mean, col.quantile), bty="n", cex = amplif.legend)
            }else{
                legend(x = (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("omd")[2] - par("omd")[1])) * par("omd")[1]), y = (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / (par("omd")[4] - par("omd")[3])) * (1 - par("omd")[4]) / 2), legend = c(c("min, Q1, Median, Q3, max"), "mean +/- 1.96sd", paste0("Trimming interval: ", paste0(trim.cutoffs, collapse = " , "))), yjust = 0, lty=1, col=c(col.box, "red", color.cut), bty="n", cex = amplif.legend, y.intersp=1.25)
            }
        }else{
            if(interval.scale.disp == TRUE){
                legend(x = (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("omd")[2] - par("omd")[1])) * par("omd")[1]), y = (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / (par("omd")[4] - par("omd")[3])) * (1 - par("omd")[4]) / 2), legend = c(c("min, Q1, Median, Q3, max"), "mean +/- sd", "Mean +/- sd multiplying factor", "Quantile"), yjust = 0, lty=1, col=c(col.box, "red", col.mean, col.quantile), bty="n", cex = amplif.legend)
            }else{
                legend(x = (par("usr")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1])) * par("plt")[1] - ((par("usr")[2] -  par("usr")[1]) / (par("omd")[2] - par("omd")[1])) * par("omd")[1]), y = (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / (par("omd")[4] - par("omd")[3])) * (1 - par("omd")[4]) / 2), legend = c(c("min, Q1, Median, Q3, max"), "mean +/- sd"), yjust = 0, lty=1, col=c(col.box, "red"), bty="n", cex = amplif.legend, y.intersp=1.25)
            }
        }
        par(xpd = FALSE, xaxs = ifelse(std.x.range, "i", "r"), yaxs = ifelse(std.y.range, "i", "r"))
        hist(as.vector(data), main = "", breaks = seq(min(as.vector(data), na.rm = TRUE), max(as.vector(data), na.rm = TRUE), length.out = length(as.vector(data)) / 10), xlim = graph.xlim, xlab = "Value", ylab="Density", col = grey(0.25))
        abline(h = par()$usr[3])
        fun.rug()
        if(interval.scale.disp == TRUE){
            fun.interval.scale.display(data.f = data)
        }
        fun.add.cut(data.f = data)
        par(xaxs = ifelse(std.x.range, "i", "r"))
        stripchart(rank(sampled.data), method="stack", vertical=FALSE, ylim=c(0.99, 1.3), group.names = "", xlab = "Rank of values", ylab="", pch=1, cex = cex.pt / 0.2)
        fun.rug(y.nb.inter.tick.f = 0)
        x.text <- par("usr")[2] + (par("usr")[2] -  par("usr")[1]) / (par("plt")[2] - par("plt")[1]) * (1 - par("plt")[2]) / 2
        y.text <- (par("usr")[4] + ((par("usr")[4] -  par("usr")[3]) / (par("plt")[4] - par("plt")[3])) * (1 - par("plt")[4]) + ((par("usr")[4] -  par("usr")[3]) / ((par()$omd[4] / 2) * ((par("plt")[4] - par("plt")[3])))) * (1 - par("omd")[4])) # BEWARE. Here in "(par()$omd[4] / 2", division by two because there are 2 graphs staked on the y axis, and not one
        par(xpd=NA)
        text(x = x.text, y = y.text, paste0(corner.text), adj=c(1, 1.1), cex = magnific.corner.text) # text at the topright corner
        par(xpd=FALSE)
        par(xaxs = ifelse(std.x.range, "i", "r"), yaxs = ifelse(std.y.range, "i", "r"))
        qqnorm(as.vector(sampled.data), main = "", datax = TRUE, ylab = "Value", pch = 1, col = "red", cex = cex.pt / 0.2)
        fun.rug()
        if(diff(quantile(as.vector(data), probs = c(0.25, 0.75), na.rm = TRUE)) != 0){ # otherwise, error generated
            qqline(as.vector(data), datax = TRUE)
        }
        if(interval.scale.disp == TRUE){
            fun.interval.scale.display(data.f = data)
        }
        fun.add.cut(data.f = data)
    }
    if(trim.return == TRUE){
        return(output)
    }
Gael  MILLOT's avatar
Gael MILLOT committed
2775
2776
2777
}


Gael  MILLOT's avatar
Gael MILLOT committed
2778
2779
2780
2781
2782
2783
2784
################ Exporting results (text & tables)


######## fun_export_data() #### Print string or data object into output file


# Check OK: clear to go Apollo
Gael  MILLOT's avatar
Gael MILLOT committed
2785
fun_export_data <- function(data = NULL, output ="results.txt", path = "C:/Users/Gael/Desktop", no.overwrite = TRUE, rownames.kept = FALSE, vector.cat = FALSE, noquote = TRUE, sep = 2){
Gael  MILLOT's avatar
Gael MILLOT committed
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
    # AIM:
    # log file function: print a character string or a data object into a same output file
    # REQUIRED FUNCTIONS
    # fun_param_check()
    # ARGUMENTS
    # data: object to print in the output file. cannot be NULL
    # output: name of the output file
    # path: location of the output file
    # no.overwrite: (logical) if output file already exists, defines if the printing is appended (default TRUE) or if the output file content is erased before printing (FALSE)
    # rownames.kept: (logical) defines whether row names have to be removed or not in small tables (less than length.rows rows)
    # vector.cat (logical). If TRUE print a vector of length > 1 using cat() instead of capture.output(). Otherwise (default FALSE) the opposite
Gael  MILLOT's avatar
Gael MILLOT committed
2797
    # noquote: (logical). If TRUE no quote are present for the characters
Gael  MILLOT's avatar
Gael MILLOT committed
2798
2799
2800
2801
2802
    # sep: number of separating lines after printed data (must be integer)
    # RETURN
    # nothing
    # EXAMPLES
    # fun_export_data()
Gael  MILLOT's avatar
Gael MILLOT committed
2803
    # fun_export_data(data = 1:3, output = "results.txt", path = "C:/Users/Gael/Desktop", no.overwrite = TRUE, rownames.kept = FALSE, vector.cat = FALSE, noquote = FALSE, sep = 2)
Gael  MILLOT's avatar
Gael MILLOT committed
2804
    # DEBUGGING
Gael  MILLOT's avatar
Gael MILLOT committed
2805
    # data = 1:3 ; output = "results.txt" ; path = "C:/Users/Gael/Desktop" ; no.overwrite = TRUE ; rownames.kept = FALSE ; vector.cat = FALSE ; noquote = FALSE ; sep = 2 # for function debugging
Gael  MILLOT's avatar
Gael MILLOT committed
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
    # required function checking
    if(length(find("fun_param_check", mode = "function")) == 0){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_export_data(): REQUIRED fun_param_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
        stop(tempo.cat)
    }
    # end required function checking
    # argument checking
    if(is.null(data)){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_export_data(): data ARGUMENT CANNOT BE NULL\n\n================\n\n")
        stop(tempo.cat)
    }
    arg.check <- NULL # for function debbuging
    checked.arg.names <- NULL # for function debbuging
    ee <- expression(arg.check <- c(arg.check, tempo$problem) , checked.arg.names <- c(checked.arg.names, tempo$param.name))
    tempo <- fun_param_check(data = output, class = "character", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = path, class = "character", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = no.overwrite, class = "logical", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = rownames.kept, class = "logical", length = 1) ; eval(ee)
    tempo <- fun_param_check(data = vector.cat, class = "logical", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2825
    tempo <- fun_param_check(data = noquote, class = "logical", length = 1) ; eval(ee)
Gael  MILLOT's avatar
Gael MILLOT committed
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
    tempo <- fun_param_check(data = sep, typeof = "integer", length = 1, double.as.integer.allowed = TRUE) ; eval(ee)
    if(any(arg.check) == TRUE){
        stop() # nothing else because print = TRUE by default in fun_param_check()
    }
    # 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_param_check_dev)) # activate this line and use the function to check arguments status and if they have been checked using fun_param_check()
    # the 4 next lines are inactivated but kept because at a time, I might have a problem with data (solved with data = NULL). These 4 lines are just to know how to detect a missing argument. Important here because if data is not provided, print the code of the data function
    # arg.user.list <- as.list(match.call(expand.dots=FALSE))[-1] # recover all the arguments provided by the function user (excluding the argument with defaults values not provided by the user. Thus, it is really the list indicated by the user)
    # default.arg.list <- formals(fun = sys.function(sys.parent())) # list of all the arguments of the function with their default values (not the values of the user !). It seems that ls() as first line of the function provide the names of the arguments (empty, called, etc., or not)
    # arg.without.default.value <- sapply(default.arg.list, is.symbol) & sapply(sapply(default.arg.list, as.character), identical, "") # logical to detect argument without default values (these are typeof "symbol" and class "name" and empty character
    # if( ! all(names(default.arg.list)[arg.without.default.value] %in% names(arg.user.list))){ # test that the arguments with no null values are provided by the user
    # tempo.cat <- paste0("\n\n================\n\nERROR IN fun_export_data(): VALUE REQUIRED FOR THESE ARGUMENTS WITH NO DEFAULTS VALUES: ", paste(names(default.arg.list)[arg.without.default.value][ ! names(default.arg.list)[arg.without.default.value] %in% names(arg.user.list)], collapse = " "), "\n\n================\n\n")
    #stop(tempo.cat)
    # }
    if(output == ""){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_export_data(): output ARGUMENT DOES NOT CORRESPOND TO A VALID CHARACTER STRING\n\n================\n\n")
        stop(tempo.cat)
    }
    if(dir.exists(path) == FALSE){
        tempo.cat <- paste0("\n\n================\n\nERROR IN fun_export_data(): path ARGUMENT DOES NOT CORRESPOND TO EXISTING DIRECTORY\n\n================\n\n")
        stop(tempo.cat)
    }
    # end argument checking
    if(all(class(data) %in% c("matrix", "data.frame", "table"))){
        if(rownames.kept == FALSE & all(class(data) == "data.frame") & nrow(data) != 0 & nrow(data) <= 4){ # for data frames with nrows <= 4
            rownames.output.tables <- ""
            length.rows <- nrow(data)
            for(i in 1:length.rows){ # replace the rownames of the first 4 rows by increasing number of spaces (beacause identical row names not allowed in data frames). This method cannot be extended to more rows as the printed data frame is shifted on the right because of "big empty rownames"
                rownames.output.tables <- c(rownames.output.tables, paste0(rownames.output.tables[i]," ", collapse=""))
            }
            row.names(data) <- rownames.output.tables[1:length.rows]
        }else if(rownames.kept == FALSE & all(class(data) %in% c("matrix", "table"))){
            rownames(data) <- rep("", nrow(data))  # identical row names allowed in matrices and tables
        }
Gael  MILLOT's avatar
Gael MILLOT committed
2859
2860
2861
2862
2863
        if(noquote == TRUE){
            capture.output(noquote(data), file=paste0(path, "/", output), append = no.overwrite)
        }else{
            capture.output(data, file=paste0(path, "/", output), append = no.overwrite)
        }
Gael  MILLOT's avatar
Gael MILLOT committed
2864
    }else if(is.vector(data) & all(class(data) != "list") & (length(data) == 1 | vector.cat == TRUE)){
Gael  MILLOT's avatar
Gael MILLOT committed
2865
2866
2867
2868
2869
        if(noquote == TRUE){
            cat(noquote(data), file= paste0(path, "/", output), append = no.overwrite)
        }else{
            cat(data, file= paste0(path, "/", output), append = no.overwrite)
        }
Gael  MILLOT's avatar
Gael MILLOT committed
2870
    }else{ # other (array, list, factor or vector with vector.cat = FALSE)
Gael  MILLOT's avatar
Gael MILLOT committed
2871
2872
2873
2874
2875
        if(noquote == TRUE){
            capture.output(noquote(data), file=paste0(path, "/", output), append = no.overwrite)
        }else{
            capture.output(data, file=paste0(path, "/", output), append = no.overwrite)
        }
Gael  MILLOT's avatar
Gael MILLOT committed
2876
2877
2878
    }
    sep.final <- paste0(rep("\n", sep), collapse = "")
    write(sep.final, file= paste0(path, "/", output), append = TRUE) # add a sep
Gael  MILLOT's avatar
Gael MILLOT committed
2879
2880
2881
}