From 816d05dcedac1dc4afbc7cf74f7a985fdf22dd7a Mon Sep 17 00:00:00 2001 From: "taha@asks2" Date: Sun, 2 Jul 2023 21:56:25 +0200 Subject: [PATCH] Separate number and SI prefix by narrow non-breakable space This looks better and more closely emulates the proper typesetting of units. Bumped patch version number. --- DESCRIPTION | 2 +- R/numeric.R | 9 +++++---- README.md | 4 ++-- man/numbers2prefix.Rd | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 67aface..d753216 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: common Type: Package Title: A collection of solarchemist's common R functions -Version: 0.1.0 +Version: 0.1.1 Description: A package containing solarchemist's commonly-used R functions and scripts. Authors@R: person("Taha", "Ahmed", email = "taha@chepec.se", role = c("aut", "cre")) License: GPL-3 + file LICENSE diff --git a/R/numeric.R b/R/numeric.R index db57903..3e073bd 100644 --- a/R/numeric.R +++ b/R/numeric.R @@ -60,7 +60,8 @@ is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) { #' @param rounding rounds number to nearest integer (default FALSE), boolean #' @param digits if rounding=FALSE, lets you specify significant figures (default 6), numeric #' -#' @return number with SI prefix (as character string) +#' @return number followed by SI prefix (as character string, +#' separated by narrow no-break space) #' @export numbers2prefix <- function(number, rounding = FALSE, digits = ifelse(rounding, NA, 6)) { # https://www.nist.gov/pml/owm/metric-si-prefixes @@ -96,12 +97,12 @@ numbers2prefix <- function(number, rounding = FALSE, digits = ifelse(rounding, N ix <- findInterval(x = number, vec = lut$factor) if (ix > 0 && ix < length(lut$factor) && lut$factor[ix] != 1) { if (rounding == TRUE && !is.numeric(digits)) { - sistring <- paste(round(number / lut$factor[ix]), lut$symbol[ix]) + sistring <- paste0(round(number / lut$factor[ix]), "\u202f", lut$symbol[ix]) } else if (rounding == TRUE || is.numeric(digits)) { - sistring <- paste(signif(number / lut$factor[ix], digits), lut$symbol[ix]) + sistring <- paste0(signif(number / lut$factor[ix], digits), "\u202f", lut$symbol[ix]) } else { - sistring <- paste(number / lut$factor[ix], lut$symbol[ix]) + sistring <- paste0(number / lut$factor[ix], "\u202f", lut$symbol[ix]) } } else { sistring <- as.character(number) diff --git a/README.md b/README.md index 5d415f4..9c7adc6 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,11 @@ I suggest the following package rebuild procedure: + Run `devtools::check()`. Should complete with no warnings, errors or notes: ``` -── R CMD check results ─────────────────────────────────────── common 0.1.0 ──── +── R CMD check results ─────────────────────────────────────── common 0.1.1 ──── Duration: 8.1s 0 errors ✔ | 0 warnings ✔ | 0 notes ✔ ``` -+ If necessary, run `devtools::document()` to update the documentation. ++ As necessary, run `devtools::document()` to update the documentation. Contributions are welcome, no matter whether code, bug reports or suggestions! diff --git a/man/numbers2prefix.Rd b/man/numbers2prefix.Rd index cd3e839..da41b0a 100644 --- a/man/numbers2prefix.Rd +++ b/man/numbers2prefix.Rd @@ -14,7 +14,8 @@ numbers2prefix(number, rounding = FALSE, digits = ifelse(rounding, NA, 6)) \item{digits}{if rounding=FALSE, lets you specify significant figures (default 6), numeric} } \value{ -number with SI prefix (as character string) +number followed by SI prefix (as character string, + separated by narrow no-break space) } \description{ Using SI unit prefixes is a more compact way to write very large or very small