Cute Little R Functions contains 33 functions for R/RStudio that facilitate basic procedures in 1) object analysis, 2) object modification, 3) graphic handling and 4) log file management.
Cute Little R Functions contains 34 functions for R/RStudio that facilitate basic procedures in 1) object analysis, 2) object modification, 3) graphic handling and 4) log file management.
The function names are:
...
...
@@ -46,6 +46,7 @@ fun_empty_graph() #### text to display for empty graphs
## gg graphics
fun_gg_palette() #### ggplot2 default color palette
fun_gg_just() #### ggplot2 justification of the axis labeling, depending on angle
# obs1 <- c("A", "B", "C", "C_modif1", "D") ; obs2 <- c("A", "A_modif1", "C") ; fun_name_change(obs1, obs2) # the function checks that the new names are neither in obs1 nor in obs2 (increment the number after the added string)
...
...
@@ -3157,6 +3160,116 @@ hcl(h = hues, l = 65, c = 100)[1:n]
}
######## fun_gg_just() #### ggplot2 justification of the axis labeling, depending on angle
# Check OK: clear to go Apollo
fun_gg_just<-function(angle,axis){
# AIM
# provide correct justification for axis labeling, depending on the chosen angle
# ARGUMENTS
# angle: integer value of the text angle for the axis labels. Positive values for counterclockwise rotation: 0 for horizontal, 90 for vertical, 180 for upside down etc. Negative values for clockwise rotation: 0 for horizontal, -90 for vertical, -180 for upside down etc.
# axis: which axis for? Either "x" or "y"
# REQUIRED PACKAGES
# none
# REQUIRED FUNCTIONS FROM CUTE_LITTLE_R_FUNCTION
# fun_param_check()
# RETURN
# a list containing:
# $angle: the submitted angle (value potentially reduced to fit the [-360 ; 360] interval, e.g., 460 -> 100, without impact on the final angle displayed)
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": REQUIRED fun_param_check() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
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 (with no arguments left as NULL) to check arguments status and if they have been checked using fun_param_check()
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": REQUIRED fun_gg_palette() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": REQUIRED fun_name_change() FUNCTION IS MISSING IN THE R ENVIRONMENT\n\n================\n\n")
stop(tempo.cat)
}
# end required function checking
# reserved words to avoid bugs (used in this function)
reserved.words<-c("fake_y","fake_categ")
# end reserved words to avoid bugs (used in this function)
# check list lengths (and names of data1 compartments if non name present)
warning<-NULL
if(all(class(data1)=="list")){
...
...
@@ -3381,6 +3502,12 @@ removed.row.nb <- vector("list", length = length(data1)) # to report NA removal
removed.rows<-vector("list",length=length(data1))# to report NA removal
for(i1in1:length(data1)){
tempo<-fun_param_check(data=data1[[i1]],data.name=ifelse(length(data1)==1,"data1",paste0("data1 NUMBER ",i1)),class="data.frame",na.contain=TRUE,fun.name=function.name);eval(ee)
# reserved word checking
if(any(names(data1[[i1]])%in%reserved.words)){# I do not use fun_name_change() because cannot control y before creating "fake_y". But ok because reserved are not that common
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": COLUMN NAMES OF ",ifelse(length(data1)==1,"data1",paste0("data1 NUMBER ",i1))," ARGUMENT CANNOT BE ONE OF THESE WORDS\n",paste(reserved.words,collapse=" "),"\nTHESE ARE RESERVED FOR THE ",function.name," FUNCTION\n\n================\n\n")
stop(tempo.cat)
}
# end reserved word checking
tempo<-fun_param_check(data=x[[i1]],data.name=ifelse(length(x)==1,"x",paste0("x NUMBER ",i1)),class="vector",mode="character",length=1,fun.name=function.name);eval(ee)
# check of geom now because required for y argument
tempo<-fun_param_check(data=geom[[i1]],data.name=ifelse(length(geom)==1,"geom",paste0("geom NUMBER ",i1)),options=c("geom_point","geom_line","geom_path","geom_hline","geom_vline"),length=1,fun.name=function.name);eval(ee)
# vertical: logical. Vertical bars? BEWARE: cannot have horizontal bars with a log axis, i.e., ylog = TRUE & vertical = FALSE (see ylog above)
# title: character string of the graph title
# text.size: numeric value of the text size (in points)
# text.angle: integer value of the text angle for the x-axis labels. Positive values for counterclockwise rotation: 0 for horizontal, 90 for vertical, 180 for upside down etc. Negative values for clockwise rotation: 0 for horizontal, -90 for vertical, -180 for upside down etc.
# classic: logical. Use the classic theme (article like)?
# grid: logical. draw horizontal lines in the background to better read the bar values? Not considered if classic = FALSE
# orientation example. Beware: log scale automatically set to FALSE for horizontal display, because of a bug in ggplot2 (https://github.com/tidyverse/ggplot2/issues/881)
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": y ARGUMENT MUST BE A COLUMN NAME OF data1\n\n================\n\n")
stop(tempo.cat)
}elseif(tempo$problem==FALSE){
if(any(y%in%reserved.words)){
if(any(duplicated(names(data1)))){
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": DUPLICATED COLUMN NAMES OF data1 ARGUMENT NOT ALLOWED:\n",paste(names(data1)[duplicated(names(data1))],collapse=" "),"\n\n================\n\n")
tempo.warning<-paste0("IN y ARGUMENT (COLUMN NAMES OF data1 ARGUMENT),\n",paste(tempo.output$ini,collapse=" "),"\nELEMENTS HAVE BEEN REPLACED BY\n",paste(tempo.output$post,collapse=" "),"\nBECAUSE RISK OF BUG AS SOME NAMES IN y ARGUMENT ARE RESERVED WORD USED BY THIS FUNCTION")
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": categ ARGUMENT MUST BE COLUMN NAMES OF data1. HERE IT IS:\n",paste(categ,collapse=" "),"\n\n================\n\n")
stop(tempo.cat)
}elseif(any(categ%in%reserved.words)){
}
# reserved word checking
if(any(names(data1)%in%reserved.words)){
if(any(duplicated(names(data1)))){
tempo.cat<-paste0("\n\n================\n\nERROR IN ",function.name,": DUPLICATED COLUMN NAMES OF data1 ARGUMENT NOT ALLOWED:\n",paste(names(data1)[duplicated(names(data1))],collapse=" "),"\n\n================\n\n")
tempo.warning<-paste0("IN y ARGUMENT (COLUMN NAMES OF data1 ARGUMENT),\n",tempo.output$ini[i3]," HAS BEEN REPLACED BY ",tempo.output$post[i3],"\nBECAUSE RISK OF BUG AS SOME NAMES IN y ARGUMENT ARE RESERVED WORD USED BY THE ",function.name," FUNCTION")
tempo.warning<-paste0("IN categ ARGUMENT (COLUMN NAMES OF data1 ARGUMENT),\n",tempo.output$ini[i3]," HAS BEEN REPLACED BY ",tempo.output$post[i3],"\nBECAUSE RISK OF BUG AS SOME NAMES IN categ ARGUMENT ARE RESERVED WORD USED BY THE ",function.name," FUNCTION")
tempo.warning<-paste0("IN categ ARGUMENT (COLUMN NAMES OF data1 ARGUMENT),\n",paste(tempo.output$ini,collapse=" "),"\nELEMENTS HAVE BEEN REPLACED BY\n",paste(tempo.output$post,collapse=" "),"\nBECAUSE RISK OF BUG AS SOME NAMES IN categ ARGUMENT ARE RESERVED WORD USED BY THIS FUNCTION")
tempo.warning<-paste0("IN COLUMN NAMES OF data1 ARGUMENT,\n",paste(tempo.output$ini,collapse=" "),"\nNAMES HAVE BEEN REPLACED BY\n",paste(tempo.output$post,collapse=" "),"\nBECAUSE RISK OF BUG AS THESE NAMES ARE RESERVED WORD USED BY THE ",function.name," FUNCTION")
@@ -4720,11 +4859,11 @@ fun_gg_boxplot <- function(data1, y, categ, class.order = NULL, legend.name = NU
# RETURN
# a boxplot
# a list of the graph info if return argument is TRUE:
# stat: the graphic statistics
# removed.row.nb: which rows have been removed due to NA detection in y and categ columns (NULL if no row removed)
# removed.rows: removed rows containing NA (NULL if no row removed)
# data: the graphic info coordinates
# warnings: the warning messages. Use cat() for proper display. NULL if no warning
# $stat: the graphic statistics
# $removed.row.nb: which rows have been removed due to NA detection in y and categ columns (NULL if no row removed)
# $removed.rows: removed rows containing NA (NULL if no row removed)
# $data: the graphic info coordinates
# $warnings: the warning messages. Use cat() for proper display. NULL if no warning
# EXAMPLES
# obs1 <- data.frame(a = 1:20, group1 = rep(c("G", "H"), times = 10)) ; fun_gg_bar_mean(data1 = obs1, y = "a", categ = "group1", categ.color = "white") # separate bars, modification of bar color 1 (a single value)
# obs1 <- data.frame(a = 1:20, group1 = rep(c("G", "H"), times = 10)) ; fun_gg_bar_mean(data1 = obs1, y = "a", categ = "group1", categ.color = c("red", "blue")) # separate bars, modification of bar color 2 (one value par class of categ2)
...
...
@@ -4754,11 +4893,11 @@ fun_gg_boxplot <- function(data1, y, categ, class.order = NULL, legend.name = NU
# obs1 <- data.frame(a = 1:1000, group1 = rep(c("G", "H"), times = 500), group2 = rep(LETTERS[1:5], each = 200)) ; fun_gg_bar_mean(data1 = obs1, y = "a", categ = c("group1", "group2"), dot.color = "grey", pt.size = 3, alpha = 1, jitter = 1) # width example. With jitter = 1, dispersion around the corresponding bar width