diff --git a/.gitignore b/.gitignore index 0556a4c..8c2ca40 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ vignettes/*.pdf *.Rproj Stringendo.Rproj Stringendo.Rproj +.DS_Store diff --git a/CITATION.cff b/CITATION.cff index d6a1a85..1f29eee 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -1,12 +1,12 @@ cff-version: 1.2.0 title: vertesy/Stringendo -version: v0.8.5 +version: v1.1.1 message: >- If you use this software, please cite it using these metadata. type: software authors: - given-names: Abel family-names: Vertesy - email: abel.vertesy@imba.oeaw.ac.at + email: av@imba.oeaw.ac.at affiliation: IMBA orcid: 'https://orcid.org/0000-0001-6075-5702' diff --git a/DESCRIPTION b/DESCRIPTION index 201f8e7..7510b18 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,10 +1,8 @@ Package: Stringendo -Title: Stringendo - string parser -Version: 0.8.5 +Title: Stringendo - string manipulation utilities +Version: 1.1.1 Authors@R: person("Abel", "Vertesy", , "av@imba.oeaw.ac.at", role = c("aut", "cre")) -Author: Abel Vertesy [aut, cre] -Maintainer: Abel Vertesy [cre] Description: Stringendo is a set of R functions to parse strings from variables and to manipulate strings. License: GPL-3 + file LICENSE @@ -20,6 +18,6 @@ Suggests: devtools, testthat Encoding: UTF-8 -Packaged: 2025-08-05 15:28:06.697097 +Packaged: 2025-12-03 10:56:21.465331 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 diff --git a/Development/CITATION.cff b/Development/CITATION.cff new file mode 100644 index 0000000..e69de29 diff --git a/Development/Create_the_Stringendo_Package.OLD.R b/Development/Create_the_Stringendo_Package.OLD.R deleted file mode 100644 index 4dc6621..0000000 --- a/Development/Create_the_Stringendo_Package.OLD.R +++ /dev/null @@ -1,141 +0,0 @@ -###################################################################################################### -# Create_the_Stringendo_Package.R -###################################################################################################### -# source("~/GitHub/Packages/Stringendo/Development/Create_the_Stringendo_Package.R") -rm(list = ls(all.names = TRUE)); -try(dev.off(), silent = TRUE) - - -# Functions ------------------------ -# require("devtools") -# require("roxygen2") -# require("stringr") - -# # devtools::install_github(repo = "vertesy/CodeAndRoll2") -# require('CodeAndRoll2') -# # try (source('~/GitHub/Packages/CodeAndRoll/CodeAndRoll.R'),silent= FALSE) # ONLY If Stringendo not yet exist -# require('Stringendo') - -# Setup ------------------------ -package.name <- "Stringendo" -package.version <- "0.5.0" -setwd("~/GitHub/Packages/") - -RepositoryDir <- paste0("~/GitHub/Packages/", package.name, "/") -fname <- paste0(package.name, ".R") -Package_FnP <- paste0(RepositoryDir, "R/", fname) - -BackupDir <- "~/GitHub/Packages/Stringendo/Development/" -dir.create(BackupDir) - -# devtools::use_package("vioplot") -DESCRIPTION <- list("Title" = "Stringendo - string parser" - , "Author" = person(given = "Abel", family = "Vertesy", email = "abel.vertesy@imba.oeaw.ac.at", role = c("aut", "cre") ) - , "Authors@R" = 'person(given = "Abel", family = "Vertesy", email = "a.vertesy@imba.oeaw.ac.at", role = c("aut", "cre") )' - , "Description" = "Stringendo is a set of R functions to parse strings from variables and to manipulate strings." - , "License" = "GPL-3 + file LICENSE" - , "Version" = package.version - , "Packaged" = Sys.time() - # , "Repository" = "CRAN" - # , "Depends" = "" - # , "Imports" = "devtools, grDevices, usethis, MarkdownReports" - , "Imports" = "clipr" - , "Suggests" = "MarkdownHelpers, MarkdownReports" - , "BugReports"= "https://github.com/vertesy/Stringendo/issues" -) - - -setwd(RepositoryDir) -if ( !dir.exists(RepositoryDir) ) { create(path = RepositoryDir, description = DESCRIPTION, rstudio = TRUE) -} else { - getwd() - try(file.remove(c("DESCRIPTION","NAMESPACE", "Stringendo.Rproj"))) - usethis::create_package(path = RepositoryDir, fields = DESCRIPTION, open = F) -} - - -# go and write fun's ------------------------------------------------------------------------ -# file.edit(Package_FnP) - -# Create Roxygen Skeletons ------------------------ -# RoxygenReady(Package_FnP) - -# replace output files ------------------------------------------------ -BackupOldFile <- (paste0(BackupDir, "Development", ".bac")) -AnnotatedFile <- (paste0(BackupDir, "Development", ".annot.R")) -file.copy(from = Package_FnP, to = BackupOldFile, overwrite = TRUE) -# file.copy(from = AnnotatedFile, to = Package_FnP, overwrite = TRUE) - -# Manual editing of descriptors ------------------------------------------------ -# file.edit(Package_FnP) - -# Compile a package ------------------------------------------------ -setwd(RepositoryDir) -getwd() -devtools::document() -warnings() - - -{ - "update cff version" - citpath <- paste0(RepositoryDir, 'CITATION.cff') - xfun::gsub_file(file = citpath, perl = T - , "^version: v.+", paste0("version: v", package.version)) -} - - -# Install your package ------------------------------------------------ -# # setwd(RepositoryDir) -# unload("Stringendo") -devtools::install(RepositoryDir, upgrade = F) - -'after uploading' -pak::pkg_install('vertesy/Stringendo') - -# require("Stringendo") -# # remove.packages("Stringendo") -# # Test your package ------------------------------------------------ -# help("wplot") -# cat("\014") -# devtools::run_examples() - - -# Test if you can install from github ------------------------------------------------ -# devtools::install_github(repo = "vertesy/Stringendo") - -# require("Stringendo") - -# Clean up if not needed anymore ------------------------------------------------ -# View(installed.packages()) -# remove.packages("Stringendo") - -check(RepositoryDir, cran = TRUE) -# as.package(RepositoryDir) -# -# -# # source("https://install-github.me/r-lib/desc") -# # library(desc) -# # desc$set("Stringendo", "foo") -# # desc$get(Stringendo) -# -# -# system("cd ~/GitHub/Stringendo/; ls -a; open .Rbuildignore") - -# Check package dependencies ------------------------------------------------ -depFile = paste0(RepositoryDir, 'Development/Dependencies.R') - -(f.deps <- NCmisc::list.functions.in.file(filename = Package_FnP)) -# clipr::write_clip(f.deps) - -sink(file = depFile); print(f.deps); sink() -p.deps <- gsub(x = names(f.deps), pattern = 'package:', replacement = '') -write(x = p.deps, file = depFile, append = T) -p.dep.declared <- trimws(unlist(strsplit(DESCRIPTION$Imports, ","))) -p.dep.new <- sort(union( p.deps, p.dep.declared)) -# clipr::write_clip(p.dep.new) - - -# Linter and Styler ------------------------------------------------ - - -styler::style_file("~/GitHub/Packages/Stringendo/R/Stringendo.R") diff --git a/Development/Create_the_Stringendo_Package.R b/Development/Create_the_Stringendo_Package.R index 9738d9f..12e8d48 100644 --- a/Development/Create_the_Stringendo_Package.R +++ b/Development/Create_the_Stringendo_Package.R @@ -21,10 +21,12 @@ source(config.path) # install.packages('ggExtra') require(PackageTools) + PackageTools::document_and_create_package(repository.dir, config_file = 'config.R') 'git add commit push to remote' + # Install your package ------------------------------------------------ "disable rprofile by" rprofile() @@ -82,8 +84,8 @@ if (F) { for (scriptX in ls.scripts.full.path) { PackageTools::list_of_funs_to_markdown(scriptX) } -file.edit(paste0(repository.dir, "R/list.of.functions.in.", package.name, ".det.md")) -file.edit(paste0(repository.dir, "README.md")) +file.edit(paste0(repository.dir, "/R/list.of.functions.in.", package.name, ".det.md")) +file.edit(paste0(repository.dir, "/README.md")) file.remove(paste0(repository.dir, "/R/list.of.functions.in.", package.name, ".det.md")) r$PackageTools() @@ -99,6 +101,7 @@ for (scriptX in ls.scripts.full.path) { } +PackageTools::document_and_create_package(repository.dir, config_file = 'config.R') diff --git a/Development/config.R b/Development/config.R index 46aeb71..ee233b3 100644 --- a/Development/config.R +++ b/Development/config.R @@ -3,8 +3,8 @@ DESCRIPTION <- list( package.name = "Stringendo", - version = "0.8.5", - title = "Stringendo - string parser", + version = "1.1.1", + title = "Stringendo - string manipulation utilities", description = "Stringendo is a set of R functions to parse strings from variables and to manipulate strings.", depends = "base", diff --git a/NAMESPACE b/NAMESPACE index a693389..da5cc90 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,7 +31,7 @@ export(fix_special_characters_bash) export(flag.name_value) export(flag.nameiftrue) export(flag.names_list) -export(flag.names_list.all.new) +export(format_number_h) export(idate) export(ifExistsAndTrue) export(ifExistsElse) @@ -39,9 +39,9 @@ export(imessage) export(iprint) export(is.character.or.NULL) export(is.numeric.or.logical) -export(knl) export(kollapse) export(kpipe) +export(kpnl) export(kpp) export(kppc) export(kppd) @@ -59,10 +59,11 @@ export(parseParamStringWNames) export(parsepvalue) export(percentage_formatter) export(percentile2value) -export(pnl) +export(ppc) export(ppcol) export(ppd) export(ppipe) +export(ppnl) export(ppp) export(pps) export(ppu) diff --git a/R/Stringendo.R b/R/Stringendo.R index 7ee71da..3847190 100644 --- a/R/Stringendo.R +++ b/R/Stringendo.R @@ -38,7 +38,7 @@ #' #' @export stopif <- function(...) { - args <- list(...) # Capture all conditions + args <- list(...) # Capture all conditions for (i in seq_along(args)) { condition <- args[[i]] @@ -57,7 +57,7 @@ stopif <- function(...) { } } - invisible() # No visible output + invisible() # No visible output } @@ -83,7 +83,7 @@ stopif <- function(...) { #' #' @export warnifnot <- function(...) { - args <- list(...) # Capture all conditions + args <- list(...) # Capture all conditions for (i in seq_along(args)) { condition <- args[[i]] @@ -93,16 +93,17 @@ warnifnot <- function(...) { if (!(is.logical(condition) && all(condition, na.rm = TRUE))) { # Use the provided name as a custom warning message if it exists, # otherwise, use the condition's expression + default_msg <- paste(deparse(match.call()[[i + 1]]), "is not TRUE") + message <- if (!is.null(name) && nzchar(name)) { - paste(name, "is not TRUE") + paste(name, "\n", default_msg, "\n") } else { - paste(deparse(match.call()[[i + 1]]), "is not TRUE") + default_msg } warning(message, call. = FALSE, immediate. = TRUE) } } - - invisible() # No visible output + invisible() } @@ -126,7 +127,7 @@ warnifnot <- function(...) { #' #' @export warnif <- function(...) { - args <- list(...) # Capture all conditions + args <- list(...) # Capture all conditions for (i in seq_along(args)) { condition <- args[[i]] @@ -136,16 +137,17 @@ warnif <- function(...) { if (is.logical(condition) && all(condition, na.rm = TRUE)) { # Use the provided name as a custom warning message if it exists, # otherwise, use the condition's expression + default_msg <- paste(deparse(match.call()[[i + 1]]), "is TRUE") + message <- if (!is.null(name) && nzchar(name)) { - paste(name, "is TRUE") + paste(name, "\n", default_msg, "\n") } else { - paste(deparse(match.call()[[i + 1]]), "is TRUE") + default_msg } warning(message, call. = FALSE, immediate. = TRUE) } } - - invisible() # No visible output + invisible() } @@ -164,10 +166,15 @@ warnif <- function(...) { #' #' @export -ifExistsAndTrue <- function(varname = "pi" ) { - x = FALSE +ifExistsAndTrue <- function(varname = "pi") { + x <- FALSE if (exists(varname)) { - if (isTRUE(get(varname))) {x = TRUE} else {x = FALSE; iprint(varname, " exists, but != TRUE; ", get(varname))} + if (isTRUE(get(varname))) { + x <- TRUE + } else { + x <- FALSE + iprint(varname, " exists, but != TRUE; ", get(varname)) + } } return(x) } @@ -182,13 +189,15 @@ ifExistsAndTrue <- function(varname = "pi" ) { #' @param alternative Value to return if `varname` is not defined. #' @param v Logical indicating whether to print informative messages. Default is `FALSE`. #' -#' @examples ifExistsElse("pi"); ifExistsElse("pi22") +#' @examples ifExistsElse("pi") +#' ifExistsElse("pi22") #' #' @export -ifExistsElse <- function(varname, alternative = "define an alternative", v = F ) { - if(!is.character(varname)) varname <- deparse(substitute(varname)) - if(v) message("Checking if ", varname, " exists.") - if(exists(varname)) get(varname) else alternative +ifExistsElse <- function(varname, alternative = "define an alternative", v = FALSE) { + .Deprecated("get0") + if (!is.character(varname)) varname <- deparse(substitute(varname)) + if (v) message("Checking if ", varname, " exists.") + if (exists(varname)) get(varname) else alternative } @@ -202,9 +211,9 @@ ifExistsElse <- function(varname, alternative = "define an alternative", v = F ) #' @return Returns `TRUE` if `x` is either a character vector or `NULL`, otherwise `FALSE`. #' #' @examples -#' is.character.or.NULL(NULL) # TRUE +#' is.character.or.NULL(NULL) # TRUE #' is.character.or.NULL("example") # TRUE -#' is.character.or.NULL(123) # FALSE +#' is.character.or.NULL(123) # FALSE #' #' @export is.character.or.NULL <- function(x) is.null(x) || is.character(x) @@ -221,9 +230,9 @@ is.character.or.NULL <- function(x) is.null(x) || is.character(x) #' @return Returns `TRUE` if `x` is either numeric or logical, otherwise `FALSE`. #' #' @examples -#' is.numeric.or.logical(123) # TRUE -#' is.numeric.or.logical(TRUE) # TRUE -#' is.numeric.or.logical("text") # FALSE +#' is.numeric.or.logical(123) # TRUE +#' is.numeric.or.logical(TRUE) # TRUE +#' is.numeric.or.logical("text") # FALSE #' #' @export is.numeric.or.logical is.numeric.or.logical <- function(x) { @@ -252,7 +261,7 @@ is.numeric.or.logical <- function(x) { testNumericCompatible <- function(x) { stopifnot(is.numeric(x) || is.character(x)) suppressWarnings({ - x_is_numeric <- !is.na(as.numeric(x)) & is.numeric(as.numeric(x)) + x_is_numeric <- !is.na(as.numeric(x)) }) return(x_is_numeric) } @@ -294,7 +303,7 @@ testNumericCompatible <- function(x) { #' #' @examples #' my_var <- 10 -#' get_object_name(my_var) # "my_var" +#' get_object_name(my_var) # "my_var" #' #' @export substitute_deparse <- function(x) deparse(substitute(x)) @@ -316,7 +325,7 @@ message2 <- function(vec) for (item in vec) message(item) #' @description A variant to message() pasting with white space, sibling of iprint(). #' @param ... Values to collapse consecutively with spaces. #' @param collapse Separator to be used for collapsing. Default: " " -#' +#' #' @examples iprint("Hello ", "you ", 3, ", ", 11, " year old kids.") #' @export @@ -345,7 +354,7 @@ iprint <- function(...) { #' Returns the current system date and time formatted as a character #' string. The default format uses dot separated components, but any #' format recognised by [base::format] can be supplied. -#' +#' #' @param Format Date format. Default: c("%Y.%m.%d_%H.%M", "%Y.%m.%d_%Hh")[2] #' #' @return A character string of the current date/time formatted according @@ -522,23 +531,46 @@ ReplaceRepeatedWhitespaces <- function(string, replacement = " ") { } - # _________________________________________________________________________________________________ #' @title ReplaceSpecialCharacters #' #' @description ReplaceSpecialCharacters replaces special characters '[]$@()' with dots. #' @param string The string potentially having special characters. #' @param replacement The character to replace special characters with. +#' Pattern = `"[[:cntrl:]@,\\|\\[\\]\\$\\(\\)\\\\/<>\\{\\}]"` #' @param remove_dots If TRUE, all dots are removed from the string (overwrites if replacement is a dot). #' @examples ReplaceSpecialCharacters(string = "obj@meta$alpha[[3]]") #' @return A string with special characters replaced by dots. #' @export ReplaceSpecialCharacters <- function(string = "obj@meta$alpha[[3]]", replacement = ".", remove_dots = FALSE) { - x <- gsub(x = string, pattern = ",|\\||\\@|\\[|\\]|\\$|\\/\\(\\)|\\\\", replacement = replacement) + # [:cntrl:] control characters + # ,:; comma, colon, semicolon + # @ at sign + # \| pipe + # \[ left bracket + # \] right bracket + # < left angle brackets + # > right angle brackets + # { left curly brackets + # } right curly brackets + + # \$ dollar sign + # \( left parenthesis + # \) right parenthesis + # \\ backslash + # / forward slash + # Pattern to replace including angle And curvy brackets. + x <- gsub(x = string, pattern = "[[:cntrl:]@,\\|\\[\\]\\$\\(\\)\\\\/<>\\{\\}]", replacement = replacement, perl = TRUE) + # x <- gsub(x = string, pattern = "[[:cntrl:],:;@\\|\\[\\]\\$\\(\\)\\\\/]", replacement = replacement, perl = TRUE) + + # Old one "[,@\\|\\[\\]\\$\\(\\)\\\\/]" x <- ReplaceRepeatedWhitespaces(x) - if (remove_dots) x <- gsub(x = x, pattern = "\\.", replacement = "") + # Replace " ." or ". " with "." + x <- gsub(x = x, pattern = " \\.", replacement = ".", perl = TRUE ) + x <- gsub(x = x, pattern = "\\. ", replacement = ".", perl = TRUE ) ReplaceRepeatedDots(x) + if (remove_dots) x <- gsub(x = x, pattern = "\\.", replacement = "") else x } @@ -572,7 +604,7 @@ AddTrailingDotIfMissing <- function(string = "stairway.to.heaven") { #' @export AddTrailingSlashIfMissing <- function(string = "stairway/to/heaven") { LastChr <- substr(string, nchar(string), nchar(string)) - if (!LastChr == "/") { + if (LastChr != "/") { string <- paste0(string, "/") } return(string) @@ -628,6 +660,14 @@ ppd <- function(...) { paste(..., sep = "-") } +# _________________________________________________________________________________________________ +#' @title Paste by a comma followed by a white space +#' +#' @param ... Multiple simple variables to parse. +#' @export +ppc <- function(...) { + paste(..., sep = ", ") +} # _________________________________________________________________________________________________ #' @title Paste by pipe (|) and white space around it @@ -644,7 +684,7 @@ ppipe <- function(...) { #' #' @param ... Multiple simple variables to parse. #' @export -pnl <- function(...) { +ppnl <- function(...) { paste(..., sep = " \n") } @@ -706,7 +746,7 @@ kppws <- function(...) { # _________________________________________________________________________________________________ -#' @title Collapse and paste by comma (and white space) +#' @title Collapse and paste by a comma followed by a white space #' @description Collapse by comma and white space (`, `) #' @param ... Multiple simple variables to parse. #' @examples kppc("A", 1:2, "end") @@ -729,9 +769,9 @@ kpipe <- function(...) { #' @title Collapse and paste by newline (`\n`) preceded by a white space #' @description Collapse by newline (`\n`) preceded by a white space #' @param ... Multiple simple variables to parse. -#' @examples knl("A", 1:2, "end") +#' @examples kpnl("A", 1:2, "end") #' @export -knl <- function(...) { +kpnl <- function(...) { paste(c(...), sep = " \n", collapse = " \n") } @@ -752,8 +792,7 @@ knl <- function(...) { #' @examples kpwNames(c("a" = 1, "b" = 2)) #' @export kpwNames <- function(x = c("a" = 1, "b" = 2), sep1 = ": ", sep2 = " | ", prefix = NULL, suffix = NULL) { - - if(is.table(x) & length(dim(x))) { + if (is.table(x) && length(dim(x)) > 0) { # Convert one dimensional table to vector preserving the names nmz <- names(x) x <- as.vector(x) @@ -792,7 +831,7 @@ kollapse <- function(..., if (print == 1) { print(paste0(c(...), collapse = collapseby)) } else if (print == 2) { - message(paste0(c(...), collapse = collapseby)) + message(paste0(c(...), collapse = collapseby)) } paste0(c(...), collapse = collapseby) } @@ -907,10 +946,15 @@ percentile2value <- function(distribution, percentile = 0.95, FirstValOverPercen #' @title parsepvalue #' @description Parse p-value from a number to a string. #' @param pvalue pvalue to parse. Default: 0.01 - +#' @param digits Number of digits to keep. Default: 2 +#' @param brackets Whether to enclose the result in brackets. Default: FALSE #' @export -parsepvalue <- function(pvalue = 0.01) paste0("(p<", pvalue, ")") -# Parse p-value from a number to a string. +parsepvalue <- function(pvalue = 0.01, digits = 2, brackets = F, prefix = F) { + pv <- paste0("p<=", signif(pvalue, digits = digits ), "") + if (brackets) paste0("(", pv, ")") else pv + if (!isFALSE(prefix)) paste0(prefix, pv) else pv +} + # _________________________________________________________________________________________________ @@ -938,7 +982,23 @@ percentage_formatter <- function(x, digitz = 3, keep.names = FALSE, prefix = NUL } -# Format numbers as human readable strings +# _________________________________________________________________________________________________ +#' @title Format numbers for human readability +#' +#' @description Convert numeric input to character strings with +#' thousands separators and configurable decimal marks. +#' +#' @param x Numeric vector to format. +#' @param digits Minimum number of significant digits to display. Passed to +#' [format()]. +#' @param big.mark Character used between groups of thousands. +#' @param decimal.mark Character used for the decimal point. +#' +#' @return A character vector containing the formatted numbers. +#' @examples +#' format_number_h(1234) +#' format_number_h(1234.56, digits = 6, decimal.mark = ",") +#' @export format_number_h <- function(x, digits = 1, big.mark = " ", decimal.mark = ".") { stopifnot(is.numeric(x)) x <- format(x, big.mark = big.mark, decimal.mark = decimal.mark, digits = digits) @@ -949,9 +1009,8 @@ format_number_h <- function(x, digits = 1, big.mark = " ", decimal.mark = ".") { # _________________________________________________________________________________________________ #' @title Identify the dominant separator in a string #' -#' @description -#' Count dots, underscores, and white spaces in a string to guess the most -#' prevalent separator. +#' @description Count dots, underscores, and white spaces in a string +#' to guess the most prevalent separator. #' #' @param string A character string to analyze. #' @return A character string identifying the dominant separator: "dot", @@ -983,10 +1042,10 @@ countDotOrUnderscoreSeparated <- function(string) { estimated_separator <- dplyr::case_when( dot_count > max(usc_count, ws_count) ~ "dot", - usc_count > max(usc_count, ws_count) ~ "underscore", + usc_count > max(dot_count, ws_count) ~ "underscore", ws_count > max(dot_count, usc_count) ~ "white space", - dot_count == 0 & usc_count == 0 & ws_count == 0 ~ "none", - dot_count == usc_count & dot_count == usc_count ~ "undecided" + dot_count == 0 && usc_count == 0 && ws_count == 0 ~ "none", + dot_count == usc_count && dot_count == ws_count ~ "undecided" ) message("Estimated separator: ", estimated_separator) @@ -1036,7 +1095,7 @@ toCamelCase <- function(input_string, words[-1] <- sapply(words[-1], function(word) { paste0(toupper(substr(word, 1, 1)), tolower(substr(word, 2, nchar(word)))) }) - if (toclipboard & require(clipr)) try(clipr::write_clip(words), silent = TRUE) + if (toclipboard && requireNamespace("clipr", quietly = TRUE)) try(clipr::write_clip(words), silent = TRUE) # Concatenate the words back together return(paste0(words, collapse = "")) @@ -1118,7 +1177,7 @@ toDotSeparated <- function(input_string, toclipboard = TRUE) { stopifnot(is.character(result), nchar(result) > 0) # Handle clipboard functionality - if (toclipboard & requireNamespace("clipr", quietly = TRUE)) try(clipr::write_clip(result), silent = TRUE) + if (toclipboard && requireNamespace("clipr", quietly = TRUE)) try(clipr::write_clip(result), silent = TRUE) message(result) } @@ -1254,7 +1313,7 @@ FixPath <- function(string = "stairway//to/heaven", ..., is.file = FALSE) { string <- ReplaceRepeatedDots(string) string <- ReplaceRepeatedSlashes(string) LastChr <- substr(string, nchar(string), nchar(string)) - if (!is.file & !LastChr == "/") { + if (!is.file && LastChr != "/") { string <- paste0(string, "/") } return(string) @@ -1475,28 +1534,34 @@ flag.nameiftrue <- function(toggle, prefix = NULL, suffix = NULL, name.if.not = # _________________________________________________________________________________________________ -#' @title flag.names_list -#' @description Returns the name and value of each element in a list of parameters. -#' @param par A list element e.g.: p$umap -#' @examples # flag.names_list(par = p$'umap.n_neighbors') -#' -#' @export -flag.names_list <- function(par) { - if (length(par)) paste(substitute(par), kppu(par), sep = "_")[[3]] -} +#' #' @title flag.names_list +#' #' @description Returns the name and value of each element in a list of parameters. +#' #' @param par A list element e.g.: p$umap +#' #' @examples # flag.names_list(par = p$'umap.n_neighbors') +#' #' +#' #' @export +#' flag.names_list <- function(par) { +#' if (length(par)) paste(substitute(par), kppu(par), sep = "_")[[3]] +#' } # _________________________________________________________________________________________________ #' @title flag.names_list.all.new #' @description Returns the name and value of each element in a list of parameters. -#' @param pl List of parameters, Default: p.hm +#' @param ls List of parameters (name, value), Default: p.hm +#' @param sep_name_val Separator name-2-value, Default: "_" +#' @param sep_elem Separator between elements, Default: "-" #' #' @export -flag.names_list.all.new <- function(pl = p.hm) { - # if (length(pl)) paste(kppu(names(pl)), kppu(pl) , sep = "_") - if (length(pl)) kppd(paste(names(pl), pl, sep = "_")) +flag.names_list <- function(ls = p.hm, sep_name_val = "_", sep_elem = "-") { + if (length(ls)) + paste(paste(names(ls), ls, sep = sep_name_val), collapse = sep_elem) } +# flag.names_list <- function(ls = p.hm, sep_elem = "_") { +# if (length(ls)) kppd(paste(names(ls), ls, sep = sep_elem)) +# } +flag.names_list.all.new <- function() .Deprecated("flag.names_list") # _________________________________________________________________________________________________ #' @title param.list.flag @@ -1525,15 +1590,14 @@ param.list.flag <- function(par = p$"umap.min_dist") { #' MyPlotname #' #' @export -parFlags <- - function(prefix = "", +parFlags <- function(prefix = "", ..., pasteflg = TRUE, collapsechar = ".") { + .deprecated("parFlags2") namez <- as.character(as.list(match.call())[-(1:2)]) val <- c(...) names(val) <- namez - # flg = names(which(as.logical.wNames(val))) # which_names() flg <- names(val)[val] print(flg) flg <- if (pasteflg) { @@ -1560,8 +1624,7 @@ parFlags <- #' MyPlotname #' #' @export -parFlags2 <- - function(prefix = ".", +parFlags2 <- function(prefix = ".", ..., pasteflg = TRUE, coll.char = ".", @@ -1581,7 +1644,6 @@ parFlags2 <- - # _________________________________________________________________________________________________ #' @title break.lines for plot titles #' @@ -1589,14 +1651,13 @@ parFlags2 <- #' @param max.char Max characters per line #' @examples ww.break.lines(char.vec = kppd(LETTERS)) #' -#' @export +#' @export ww.break.lines ww.break.lines <- function(char.vec, max.char = 50) { gsub(pattern = paste0("(.{", max.char, "})"), "\\1\n", char.vec) } - # _________________________________________________________________________________________________ #' @title FormatAsExcelLink #' diff --git a/README.md b/README.md index a8647f2..0ec8d6b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Install directly from **GitHub** via **devtools** with one R command: ```R # install.packages("devtools"); # If you don't have it. require("devtools") -devtools::install_github(repo = "vertesy/Stringendo") +devtools::install_github(repo = "vertesy/Stringendo", ref = "main") ``` ...then simply load the package: @@ -38,210 +38,233 @@ source("https://raw.githubusercontent.com/vertesy/Stringendo/main/R/Stringendo.R
-## List of Functions in Stringendo.R (67) -Updated: 2024/10/24 11:02 - +## List of Functions in Stringendo.R (76) +Updated: 2025/12/03 10:53 - #### 1 `stopif()` Stop execution if condition is TRUE. The `stopif()` function stops the execution if the condition is `TRUE`. It is the opposite of `stopifnot()`, which stops if the condition is not `TRUE`. This function is useful to increase clarity in the code by removing double negations. - #### 2 `warnifnot()` -Issue warnings if conditions are not TRUE. The `warningifnot()` function checks whether each condition passed to it is `TRUE`. If any condition is not met, a warning is issued but the execution continues. This is similar to `stopifnot()`, which throws an error and halts execution, but `warningifnot()` only issues a warning, allowing the program to proceed. +Issue warnings if conditions are not TRUE. The `warnifnot()` function checks whether each condition passed to it is `TRUE`. If any condition is not met, a warning is issued but execution continues. This is similar to `stopifnot()`, which throws an error and halts execution, but `warnifnot()` only issues a warning, allowing the program to proceed. - #### 3 `warnif()` -Issue a warning if condition is TRUE. The `warnif()` function issues a warning if the condition is `TRUE`. It is the opposite of `warningifnot()`, which warns if the condition is not `TRUE`. This function is useful for issuing warnings when a certain condition is met. +Issue a warning if condition is TRUE. The `warnif()` function issues a warning if the condition is `TRUE`. It is the opposite of `warnifnot()`, which warns if the condition is not `TRUE`. This function is useful for issuing warnings when a certain condition is met. + +- #### 4 `ifExistsAndTrue()` +Check whether a variable exists and is TRUE. Returns `TRUE` if `varname` exists in the current environment and evaluates to `TRUE`. If the variable is missing or not `TRUE`, the function returns `FALSE` and prints a message describing the problem. + +- #### 5 `ifExistsElse()` +Return a variable's value or a default if it does not exist. Returns the value of `varname` when it exists; otherwise returns `alternative`. When `v` is `TRUE`, a message is printed indicating whether the variable was found. + +- #### 6 `is.character.or.NULL()` +Check if Input is Character or NULL. `is.character.or.NULL()` verifies if the provided input is either a character vector or NULL. -- #### 4 `testNumericCompatible()` +- #### 7 `is.numeric.or.logical()` +Check if Input is Numeric or Logical. `is.numeric.or.logical()` checks if the provided input is either numeric or logical. + +- #### 8 `testNumericCompatible()` Test if a Variable is Inherently Numeric ('0.1' as numeric). This function checks if a given variable is inherently numeric. It returns TRUE if the variable can be converted to a numeric value without loss of information and is not inherently a character string, otherwise it returns FALSE. -- #### 5 `"%!in%"()` +- #### 9 `"%!in%"()` Negation of the `in` (w. grapes) operator. `%!in%` is used to test if elements of one vector are not present in another vector. It is the negation of the `%in%` operator. This operator returns `TRUE` for elements of `x` that are not in `y`. -- #### 6 `message2()` +- #### 10 `substitute_deparse()` +Get Object Name as String. `get_object_name()` captures the name of an input object and returns it as a string. Replace `deparse\s*\(\s*substitute\s*\(([^()]+)\)\s*\)` to `substitute_deparse($1)`, then `substitute\s*\(([^()]+)\)\s*\)` to the same. + +- #### 11 `message2()` Message without collapsing. This function prints a message for each element in a character vector, instead of collapsing them into a single line as done by, `message()`. -- #### 7 `imessage()` +- #### 12 `imessage()` imessage. A variant to message() pasting with white space, sibling of iprint(). -- #### 8 `HasNames()` +- #### 13 `iprint()` iprint. A more intelligent printing function that collapses any variable passed to it by white spaces. -- #### 9 `substrRight()` +- #### 14 `HasNames()` +Parse current date, dot separated.. Returns the current system date and time formatted as a character string. The default format uses dot separated components, but any format recognised by [base::format] can be supplied. + +- #### 15 `substrRight()` substrRight. Take the right substring of a string -- #### 10 `ReplaceRepeatedDots()` -ReplaceRepeatedDots. ReplaceRepeatedDots removes multiple consecutive slashes (e.g. '..') from a string (file path). +- #### 16 `ReplaceRepeatedDots()` +ReplaceRepeatedDots. ReplaceRepeatedDots collapses multiple consecutive dots (periods) in a string into a single dot. -- #### 11 `RemoveFinalDot()` +- #### 17 `RemoveFinalDot()` RemoveFinalDot. RemoveFinalDot removes the final dot from a string -- #### 12 `RemoveInitialDot()` +- #### 18 `RemoveInitialDot()` RemoveInitialDot. RemoveInitialDot removes the initial dot from a string. -- #### 13 `RemoveTrailingDots()` +- #### 19 `RemoveTrailingDots()` RemoveTrailingDots. RemoveTrailingDots removes dots at the beginning and end of a string. -- #### 14 `ReplaceRepeatedSlashes()` +- #### 20 `ReplaceRepeatedSlashes()` ReplaceRepeatedSlashes. ReplaceRepeatedSlashes replaces multiple consecutive slashes with a single slash. -- #### 15 `RemoveFinalSlash()` +- #### 21 `RemoveFinalSlash()` RemoveFinalSlash. RemoveFinalSlash removes the final slash(es) from a string (file path). -- #### 16 `ReplaceRepeatedUnderscores()` -ReplaceRepeatedUnderscores. ReplaceRepeatedUnderscores replaces multiple consecutive slashes with a single slash. +- #### 22 `ReplaceRepeatedUnderscores()` +ReplaceRepeatedUnderscores. ReplaceRepeatedUnderscores replaces multiple consecutive underscores with a single underscore. -- #### 17 `RemoveFinalUnderscores()` -RemoveFinalUnderscores. RemoveFinalUnderscores removes the final slash(es) from a string (file path). +- #### 23 `RemoveFinalUnderscores()` +RemoveFinalUnderscores. RemoveFinalUnderscores removes trailing underscore(s) from a string. -- #### 18 `RemoveWhitespaces()` -RemoveWhitespaces. RemoveWhitespaces replaces any nr of white spaces. +- #### 24 `RemoveWhitespaces()` +RemoveWhitespaces. RemoveWhitespaces removes all whitespace characters from a string or replaces them with a specified value. -- #### 19 `ReplaceRepeatedWhitespaces()` -ReplaceRepeatedWhitespaces. ReplaceRepeatedWhitespaces replaces multiple consecutive white spaces with a single one. +- #### 25 `ReplaceRepeatedWhitespaces()` +ReplaceRepeatedWhitespaces. ReplaceRepeatedWhitespaces collapses multiple consecutive whitespace characters into a single replacement. -- #### 20 `ReplaceSpecialCharacters()` +- #### 26 `ReplaceSpecialCharacters()` ReplaceSpecialCharacters. ReplaceSpecialCharacters replaces special characters '[]$@()' with dots. -- #### 21 `AddTrailingDotIfMissing()` +- #### 27 `AddTrailingDotIfMissing()` AddTrailingDotIfMissing. Adds a trailing dot ('.') to a string if it is missing. -- #### 22 `AddTrailingSlashIfMissing()` +- #### 28 `AddTrailingSlashIfMissing()` AddTrailingSlashIfMissing. Adds a trailing slash ('/') to a string if it is missing. -- #### 23 `ppp()` +- #### 29 `ppp()` Paste by point. Paste by point -- #### 24 `pps()` +- #### 30 `pps()` Paste by (forward) slash. Paste by (forward) slash -- #### 25 `ppu()` +- #### 31 `ppcol()` +Paste by colon symbol.. Paste by colon symbol. "ppc" reserved for "comma". + +- #### 32 `ppu()` Paste by underscore. Paste by underscore -- #### 26 `pnl()` +- #### 33 `ppnl()` Paste by dash. Paste by dash -- #### 27 `kpp()` -Collapse and paste by point. Collapse by point +- #### 34 `kpp()` +Collapse and paste by point. Collapse by period (`.`) -- #### 28 `kppu()` -Collapse and paste by underscore. Collapse by underscore +- #### 35 `kppu()` +Collapse and paste by underscore. Collapse by underscore (`_`) -- #### 29 `kpps()` -Collapse and paste by (forward) slash. Collapse by (forward) slash +- #### 36 `kpps()` +Collapse and paste by (forward) slash. Collapse by forward slash (`/`) -- #### 30 `kppd()` -Collapse and paste by dash. Collapse by dash +- #### 37 `kppd()` +Collapse and paste by dash. Collapse by dash (`-`) -- #### 31 `kppws()` -Collapse and paste by white space. Collapse by white space +- #### 38 `kppws()` +Collapse and paste by white space. Collapse by white space (` `) -- #### 32 `kppc()` -Collapse and paste by comma (and white space). Collapse by white space +- #### 39 `kppc()` +Collapse and paste by a comma followed by a white space. Collapse by comma and white space (`, `) -- #### 33 `kpipe()` -Collapse and paste by pipe (|) and white spaces around it. Collapse by white space +- #### 40 `kpipe()` +Collapse and paste by pipe (|) and white spaces around it. Collapse by pipe (`|`) with surrounding spaces -- #### 34 `knl()` -Collapse and paste by newline (`\n`) preceded by a white space. Collapse by white space +- #### 41 `kpnl()` +Collapse and paste by newline (`\n`) preceded by a white space. Collapse by newline (`\n`) preceded by a white space -- #### 35 `kpwNames()` +- #### 42 `kpwNames()` Collapse and paste Elements With Names. This function takes a named vector and returns a string where each element is pasted with its name. Elements are separated by a specified string, and name-element pairs are also separated by a specified string. The default named vector is `c('a' = 1, 'b' = 2)`. -- #### 36 `kollapse()` +- #### 43 `kollapse()` Kollapse. Collapses values and strings to one string (without a white space). It also prints the results (good for a quick check) -- #### 37 `sppp()` +- #### 44 `sppp()` Simplified Paste by point. Simplified Paste by point -- #### 38 `spps()` +- #### 45 `spps()` Simplified Paste by fwd slash. Simplified Paste by fwd slash -- #### 39 `sppu()` +- #### 46 `sppu()` Simplified Paste by underscore. Simplified Paste by underscore -- #### 40 `percentile2value()` +- #### 47 `pad.na()` +pad.na. This function fills up a vector to a given length by appending NA-values at the end. If the input vector's length is less than the provided length, the function pads the vector with NA. If the vector's length is already equal to or greater than the given length, no change will be made. + +- #### 48 `percentile2value()` percentile2value. Calculate what is the actual value of the N-th percentile in a distribution or set of numbers. Useful for calculating cutoffs, and displaying them by whist()s "vline" parameter. -- #### 41 `parsepvalue()` +- #### 49 `parsepvalue()` parsepvalue. Parse p-value from a number to a string. -- #### 42 `format_number_h()` +- #### 50 `percentage_formatter()` percentage_formatter. Parse a string of 0-100% from a number between 0 and 1. -- #### 43 `countDotOrUnderscoreSeparated()` -Count Dots or Underscores in a String and return. This function counts the number of "." characters in a given string. +- #### 51 `format_number_h()` +Format numbers for human readability. Convert numeric input to character strings with thousands separators and configurable decimal marks. + +- #### 52 `countDotOrUnderscoreSeparated()` +Identify the dominant separator in a string. Count dots, underscores, and white spaces in a string to guess the most prevalent separator. -- #### 44 `toCamelCase()` +- #### 53 `toCamelCase()` Convert a String to camelCase. This function takes a string as input and converts it to camelCase format. It splits the string into words using dots as separators, capitalizes the first letter of each word (except the first word), and then concatenates them back together. -- #### 45 `toUnderscoreSeparated()` +- #### 54 `toUnderscoreSeparated()` Convert a String to underscore_separated Format. This function converts a string from camelCase or dot-separated format to an underscore-separated format. It can handle strings that are a combination of camelCase and dot-separated formats. The function replaces dots with underscores and inserts an underscore before any uppercase letter that follows a lowercase letter. It then converts all characters to lowercase. -- #### 46 `toDotSeparated()` +- #### 55 `toDotSeparated()` Convert String to Dot Separated Name. Converts a string from camelCase or underscore_separated format to dot.separated.name format. Inserts dots before each uppercase letter (except if it's the first character) or replaces underscores with dots, and then converts the entire string to lowercase. -- #### 47 `toSentence()` +- #### 56 `toSentence()` Convert CamelCase to Sentence. Takes a camelCase string and converts it to a sentence format: space-separated, with the first letter capitalized and no period at the end. -- #### 48 `fix_special_characters_bash()` +- #### 57 `fix_special_characters_bash()` Fix Special Characters for Bash. This function takes a string representing a path and escapes certain special characters to make it compatible with Bash. Specifically, it escapes spaces, opening parentheses, and closing parentheses by placing a backslash before them. -- #### 49 `ParseFullFilePath()` +- #### 58 `ParseFullFilePath()` Parse Full File Path. Constructs a full file path by combining a path, file name, and extension. It applies string clean-up operations to each component and ensures proper formatting. -- #### 50 `FixUnderscores()` -FixUnderscores. FixUnderscores removes multiple consecutive underscores (e.g. '_') from a string, and optionally also removes a final '_'. +- #### 59 `FixUnderscores()` +FixUnderscores. FixUnderscores removes multiple consecutive underscores from a string and optionally trims a trailing underscore. -- #### 51 `FixPath()` +- #### 60 `FixPath()` FixPath. FixPath removes multiple consecutive slashes (e.g. '//') from a string and adds a final '/' if missing from a file path. -- #### 52 `FixPlotName()` +- #### 61 `FixPlotName()` FixPlotName. FixPlotName replaces special characters in an input string (dollar-, at-, bracket-signs) -- #### 53 `ParseDirPath()` +- #### 62 `ParseDirPath()` ParseDirPath. ParseDirPath pastes elements by slash, then removes Double Slashes '//' from a string and adds a final '/' if missing from a file path. -- #### 54 `PasteDirNameFromFlags()` +- #### 63 `PasteDirNameFromFlags()` PasteDirNameFromFlags. Paste a dot (point) separated string from a list of inputs (that can be empty), and clean up the output string from dot multiplets (e.g: ..). -- #### 55 `extPDF()` +- #### 64 `extPDF()` extPDF. add '.pdf' as extension to a file name -- #### 56 `extPNG()` +- #### 65 `extPNG()` extPNG. add '.png' as extension to a file name -- #### 57 `parseParamStringWNames()` +- #### 66 `parseParamStringWNames()` Parse Parameter String with Names. This function parses a named vector and intermingles the names and values into a single string, with specified separators for the odd and even elements. -- #### 58 `params.2.fname()` +- #### 67 `params.2.fname()` Convert Named Parameters to Filename. This function takes named parameters and converts them into a filename string with specified separators and collapse characters. It excludes any parameters with NULL values. -- #### 59 `param.list.2.fname()` +- #### 68 `param.list.2.fname()` param.list.2.fname. Take a list of parameters and parse a string from their names and values. -- #### 60 `PasteOutdirFromFlags()` +- #### 69 `PasteOutdirFromFlags()` PasteOutdirFromFlags. Paste OutDir from (1) a path and (2) a from a list of inputs (that can be empty), and clean up the output string from dot and forward slash multiplets (e.g: ..). -- #### 61 `flag.name_value()` +- #### 70 `flag.name_value()` flag.name_value. Returns the name and its value, if its not FALSE. -- #### 62 `flag.nameiftrue()` +- #### 71 `flag.nameiftrue()` flag.nameiftrue. Returns the name and its value, if its TRUE. -- #### 63 `flag.names_list()` +- #### 72 `flag.names_list()` flag.names_list. Returns the name and value of each element in a list of parameters. -- #### 64 `flag.names_list.all.new()` +- #### 73 `flag.names_list.all.new()` flag.names_list.all.new. Returns the name and value of each element in a list of parameters. -- #### 65 `param.list.flag()` +- #### 74 `param.list.flag()` param.list.flag. Returns the name and value of each element in a list of parameters. -- #### 66 `()` +- #### 75 `parFlags()` parFlags. Create a string from the names of the (boolean) parameters (TRUE or FALSE) of true values. Use it for Suffixing plot names with the parameters that were used for that plot. -- #### 67 `FormatAsExcelLink()` +- #### 76 `FormatAsExcelLink()` parFlags2. Create a string from the names of the (boolean) parameters (TRUE or FALSE) of true values. Use it for Suffixing plot names with the parameters that were used for that plot. - - - diff --git a/man/ReplaceSpecialCharacters.Rd b/man/ReplaceSpecialCharacters.Rd index ddb3b81..f7239ce 100644 --- a/man/ReplaceSpecialCharacters.Rd +++ b/man/ReplaceSpecialCharacters.Rd @@ -13,7 +13,8 @@ ReplaceSpecialCharacters( \arguments{ \item{string}{The string potentially having special characters.} -\item{replacement}{The character to replace special characters with.} +\item{replacement}{The character to replace special characters with. +Pattern = \verb{"[[:cntrl:]@,\\\\|\\\[\\\]\\\\$\\\\(\\\\)\\\\\\\\/<>\\\\\{\\\\\}]"}} \item{remove_dots}{If TRUE, all dots are removed from the string (overwrites if replacement is a dot).} } diff --git a/man/countDotOrUnderscoreSeparated.Rd b/man/countDotOrUnderscoreSeparated.Rd index 946ce38..f7de0ab 100644 --- a/man/countDotOrUnderscoreSeparated.Rd +++ b/man/countDotOrUnderscoreSeparated.Rd @@ -14,8 +14,8 @@ A character string identifying the dominant separator: "dot", "underscore", "white space", "none", or "undecided". } \description{ -Count dots, underscores, and white spaces in a string to guess the most -prevalent separator. +Count dots, underscores, and white spaces in a string +to guess the most prevalent separator. } \examples{ \dontrun{ diff --git a/man/flag.names_list.Rd b/man/flag.names_list.Rd index 677df49..f8ca3ae 100644 --- a/man/flag.names_list.Rd +++ b/man/flag.names_list.Rd @@ -2,17 +2,28 @@ % Please edit documentation in R/Stringendo.R \name{flag.names_list} \alias{flag.names_list} -\title{flag.names_list} +\title{flag.names_list.all.new} \usage{ -flag.names_list(par) +flag.names_list(ls = p.hm, sep_name_val = "_", sep_elem = "-") } \arguments{ -\item{par}{A list element e.g.: p$umap} +\item{ls}{List of parameters (name, value), Default: p.hm} + +\item{sep_name_val}{Separator name-2-value, Default: "_"} + +\item{sep_elem}{Separator between elements, Default: "-"} } \description{ Returns the name and value of each element in a list of parameters. } -\examples{ -# flag.names_list(par = p$'umap.n_neighbors') - +\details{ +#' @title flag.names_list +#' @description Returns the name and value of each element in a list of parameters. +#' @param par A list element e.g.: p$umap +#' @examples # flag.names_list(par = p$'umap.n_neighbors') +#' +#' @export +flag.names_list <- function(par) { +if (length(par)) paste(substitute(par), kppu(par), sep = "_")[\link{3}] +} } diff --git a/man/flag.names_list.all.new.Rd b/man/flag.names_list.all.new.Rd deleted file mode 100644 index 8c03bc1..0000000 --- a/man/flag.names_list.all.new.Rd +++ /dev/null @@ -1,14 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/Stringendo.R -\name{flag.names_list.all.new} -\alias{flag.names_list.all.new} -\title{flag.names_list.all.new} -\usage{ -flag.names_list.all.new(pl = p.hm) -} -\arguments{ -\item{pl}{List of parameters, Default: p.hm} -} -\description{ -Returns the name and value of each element in a list of parameters. -} diff --git a/man/format_number_h.Rd b/man/format_number_h.Rd new file mode 100644 index 0000000..08430b8 --- /dev/null +++ b/man/format_number_h.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Stringendo.R +\name{format_number_h} +\alias{format_number_h} +\title{Format numbers for human readability} +\usage{ +format_number_h(x, digits = 1, big.mark = " ", decimal.mark = ".") +} +\arguments{ +\item{x}{Numeric vector to format.} + +\item{digits}{Minimum number of significant digits to display. Passed to +\code{\link[=format]{format()}}.} + +\item{big.mark}{Character used between groups of thousands.} + +\item{decimal.mark}{Character used for the decimal point.} +} +\value{ +A character vector containing the formatted numbers. +} +\description{ +Convert numeric input to character strings with +thousands separators and configurable decimal marks. +} +\examples{ +format_number_h(1234) +format_number_h(1234.56, digits = 6, decimal.mark = ",") +} diff --git a/man/ifExistsElse.Rd b/man/ifExistsElse.Rd index f6c5d92..f10d23c 100644 --- a/man/ifExistsElse.Rd +++ b/man/ifExistsElse.Rd @@ -4,7 +4,7 @@ \alias{ifExistsElse} \title{Return a variable's value or a default if it does not exist} \usage{ -ifExistsElse(varname, alternative = "define an alternative", v = F) +ifExistsElse(varname, alternative = "define an alternative", v = FALSE) } \arguments{ \item{varname}{Character string naming the variable to retrieve.} @@ -18,6 +18,7 @@ Returns the value of \code{varname} when it exists; otherwise returns \code{alte When \code{v} is \code{TRUE}, a message is printed indicating whether the variable was found. } \examples{ -ifExistsElse("pi"); ifExistsElse("pi22") +ifExistsElse("pi") +ifExistsElse("pi22") } diff --git a/man/is.character.or.NULL.Rd b/man/is.character.or.NULL.Rd index 9754862..2d2be40 100644 --- a/man/is.character.or.NULL.Rd +++ b/man/is.character.or.NULL.Rd @@ -16,8 +16,8 @@ Returns \code{TRUE} if \code{x} is either a character vector or \code{NULL}, oth \code{is.character.or.NULL()} verifies if the provided input is either a character vector or NULL. } \examples{ -is.character.or.NULL(NULL) # TRUE +is.character.or.NULL(NULL) # TRUE is.character.or.NULL("example") # TRUE -is.character.or.NULL(123) # FALSE +is.character.or.NULL(123) # FALSE } diff --git a/man/is.numeric.or.logical.Rd b/man/is.numeric.or.logical.Rd index fd6138f..156f091 100644 --- a/man/is.numeric.or.logical.Rd +++ b/man/is.numeric.or.logical.Rd @@ -16,8 +16,8 @@ Returns \code{TRUE} if \code{x} is either numeric or logical, otherwise \code{FA \code{is.numeric.or.logical()} checks if the provided input is either numeric or logical. } \examples{ -is.numeric.or.logical(123) # TRUE -is.numeric.or.logical(TRUE) # TRUE -is.numeric.or.logical("text") # FALSE +is.numeric.or.logical(123) # TRUE +is.numeric.or.logical(TRUE) # TRUE +is.numeric.or.logical("text") # FALSE } diff --git a/man/knl.Rd b/man/kpnl.Rd similarity index 85% rename from man/knl.Rd rename to man/kpnl.Rd index ab7b4c2..7ea0fa6 100644 --- a/man/knl.Rd +++ b/man/kpnl.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/Stringendo.R -\name{knl} -\alias{knl} +\name{kpnl} +\alias{kpnl} \title{Collapse and paste by newline (\verb{\\n}) preceded by a white space} \usage{ -knl(...) +kpnl(...) } \arguments{ \item{...}{Multiple simple variables to parse.} @@ -13,5 +13,5 @@ knl(...) Collapse by newline (\verb{\\n}) preceded by a white space } \examples{ -knl("A", 1:2, "end") +kpnl("A", 1:2, "end") } diff --git a/man/kppc.Rd b/man/kppc.Rd index 3a852a4..3459f2a 100644 --- a/man/kppc.Rd +++ b/man/kppc.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/Stringendo.R \name{kppc} \alias{kppc} -\title{Collapse and paste by comma (and white space)} +\title{Collapse and paste by a comma followed by a white space} \usage{ kppc(...) } diff --git a/man/parsepvalue.Rd b/man/parsepvalue.Rd index 9528c0b..8af9268 100644 --- a/man/parsepvalue.Rd +++ b/man/parsepvalue.Rd @@ -4,10 +4,14 @@ \alias{parsepvalue} \title{parsepvalue} \usage{ -parsepvalue(pvalue = 0.01) +parsepvalue(pvalue = 0.01, digits = 2, brackets = F, prefix = F) } \arguments{ \item{pvalue}{pvalue to parse. Default: 0.01} + +\item{digits}{Number of digits to keep. Default: 2} + +\item{brackets}{Whether to enclose the result in brackets. Default: FALSE} } \description{ Parse p-value from a number to a string. diff --git a/man/ppc.Rd b/man/ppc.Rd new file mode 100644 index 0000000..4cbeb30 --- /dev/null +++ b/man/ppc.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Stringendo.R +\name{ppc} +\alias{ppc} +\title{Paste by a comma followed by a white space} +\usage{ +ppc(...) +} +\arguments{ +\item{...}{Multiple simple variables to parse.} +} +\description{ +Paste by a comma followed by a white space +} diff --git a/man/pnl.Rd b/man/ppnl.Rd similarity index 86% rename from man/pnl.Rd rename to man/ppnl.Rd index a493b49..6dc968d 100644 --- a/man/pnl.Rd +++ b/man/ppnl.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/Stringendo.R -\name{pnl} -\alias{pnl} +\name{ppnl} +\alias{ppnl} \title{Paste by new line} \usage{ -pnl(...) +ppnl(...) } \arguments{ \item{...}{Multiple simple variables to parse.} diff --git a/man/substitute_deparse.Rd b/man/substitute_deparse.Rd index 025b4b1..05ea701 100644 --- a/man/substitute_deparse.Rd +++ b/man/substitute_deparse.Rd @@ -19,6 +19,6 @@ Replace \verb{deparse\\s*\\(\\s*substitute\\s*\\(([^()]+)\\)\\s*\\)} to \verb{su } \examples{ my_var <- 10 -get_object_name(my_var) # "my_var" +get_object_name(my_var) # "my_var" } diff --git a/tests/testthat/test-format_number_h.R b/tests/testthat/test-format_number_h.R new file mode 100644 index 0000000..23114ad --- /dev/null +++ b/tests/testthat/test-format_number_h.R @@ -0,0 +1,8 @@ +test_that("format_number_h formats numbers with separators", { + expect_equal(format_number_h(1234), "1 234") + expect_equal(format_number_h(1234.56, digits = 6, decimal.mark = ","), "1 234,56") +}) + +test_that("format_number_h errors on non-numeric input", { + expect_error(format_number_h("a")) +}) diff --git a/tests/testthat/test-percentage_formatter.R b/tests/testthat/test-percentage_formatter.R index 02cfe47..07a320b 100644 --- a/tests/testthat/test-percentage_formatter.R +++ b/tests/testthat/test-percentage_formatter.R @@ -3,8 +3,8 @@ test_that("standard numeric input yields a formatted percentage", { }) test_that("NA and NaN inputs return NA and NaN strings respectively", { - expect_identical(percentage_formatter(NA), NA) - expect_identical(percentage_formatter(NaN), NaN) + expect_identical(percentage_formatter(NA), NA_character_) + expect_identical(percentage_formatter(NaN), "NaN") }) test_that("named vectors retain names when keep.names = TRUE", {