From 8d7b2d4fbe578a04d273a361bb559bb0d989826f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20=C3=9Acar?= Date: Thu, 1 Aug 2024 19:58:09 +0200 Subject: [PATCH] new arg, rename option, some refactoring --- R/print.R | 27 +++++++++++---------------- man/format.errors.Rd | 17 ++++++++++------- tests/testthat/test-print.R | 11 +++-------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/R/print.R b/R/print.R index a91fe0e..4561590 100644 --- a/R/print.R +++ b/R/print.R @@ -4,16 +4,16 @@ #' #' @param x an \code{errors} object. #' @param digits how many significant digits are to be used for uncertainties. -#' The default, \code{NULL}, uses \code{getOption("errors.digits", 1)}. #' Use \code{digits="pdg"} to choose an appropriate number of digits for each #' value according to the Particle Data Group rounding rule (see references). #' @param scientific logical specifying whether the elements should be #' encoded in scientific format. #' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"} #' are supported through the \code{"errors.notation"} option. -#' When using the "parenthesis" error notation, by default the uncertainty -#' is formatted without any decimal point, unless the option -#' \code{"errors.parenthesis.unc.dec.point"} is set to TRUE. +#' @param decimals logical specifying whether the uncertainty should be formatted +#' with a decimal point even when the \code{"parenthesis"} notation is used. +#' Otherwise (by default), the \code{"parenthesis"} notation scales the +#' uncertainty to match the least significant digit of the value. #' @param ... ignored. #' #' @references @@ -23,6 +23,7 @@ #' x <- set_errors(1:3*100, 1:3*100 * 0.05) #' format(x) #' format(x, digits=2) +#' format(x, digits=2, decimals=TRUE) #' format(x, scientific=TRUE) #' format(x, notation="plus-minus") #' @@ -31,15 +32,14 @@ #' #' @export format.errors = function(x, - digits = NULL, + digits = getOption("errors.digits", 1), scientific = FALSE, notation = getOption("errors.notation", "parenthesis"), + decimals = getOption("errors.decimals", FALSE), ...) { stopifnot(notation %in% c("parenthesis", "plus-minus")) - if (is.null(digits)) - digits <- getOption("errors.digits", 1) digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x)) scipen <- getOption("scipen", 0) @@ -49,7 +49,7 @@ format.errors = function(x, e <- signif(.e(x), digits) nulle <- e == 0 & !is.na(e) eexp <- get_exponent(e) - xexp <- ifelse(.v(x)== 0, eexp+1, get_exponent(x)) + xexp <- ifelse(.v(x) == 0, eexp + 1, get_exponent(x)) value_digits <- ifelse(e, digits - eexp, digits) value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x)) value <- ifelse(is.finite(value), value, .v(x)) @@ -64,14 +64,9 @@ format.errors = function(x, if (notation == "parenthesis") { sep <- "(" append[] <- ")" - if (!getOption("errors.parenthesis.unc.dec.point", FALSE)) { - ## remove decimal point from uncertainty by scaling it appropriately - e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)] - } else { - ## convert uncertainty for printing, keeping decimal point in line with value - e_scale_flag = (cond & eexp < xexp) | (!cond & is.finite(e) & eexp<0) - e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag] - } + e_scale_flag <- if (!isTRUE(decimals)) is.finite(e) else + (cond & eexp < xexp) | (!cond & is.finite(e) & eexp < 0) + e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag] } else { sep <- paste0(" ", .pm, " ") prepend[cond] <- "(" diff --git a/man/format.errors.Rd b/man/format.errors.Rd index 85140b3..0e17b1e 100644 --- a/man/format.errors.Rd +++ b/man/format.errors.Rd @@ -4,14 +4,14 @@ \alias{format.errors} \title{Encode \code{errors}} \usage{ -\method{format}{errors}(x, digits = NULL, scientific = FALSE, - notation = getOption("errors.notation", "parenthesis"), ...) +\method{format}{errors}(x, digits = getOption("errors.digits", 1), + scientific = FALSE, notation = getOption("errors.notation", + "parenthesis"), decimals = getOption("errors.decimals", FALSE), ...) } \arguments{ \item{x}{an \code{errors} object.} \item{digits}{how many significant digits are to be used for uncertainties. -The default, \code{NULL}, uses \code{getOption("errors.digits", 1)}. Use \code{digits="pdg"} to choose an appropriate number of digits for each value according to the Particle Data Group rounding rule (see references).} @@ -19,10 +19,12 @@ value according to the Particle Data Group rounding rule (see references).} encoded in scientific format.} \item{notation}{error notation; \code{"parenthesis"} and \code{"plus-minus"} -are supported through the \code{"errors.notation"} option. -When using the "parenthesis" error notation, by default the uncertainty -is formatted without any decimal point, unless the option -\code{"errors.parenthesis.unc.dec.point"} is set to TRUE.} +are supported through the \code{"errors.notation"} option.} + +\item{decimals}{logical specifying whether the uncertainty should be formatted +with a decimal point even when the \code{"parenthesis"} notation is used. +Otherwise (by default), the \code{"parenthesis"} notation scales the +uncertainty to match the least significant digit of the value.} \item{...}{ignored.} } @@ -33,6 +35,7 @@ Format an \code{errors} object for pretty printing. x <- set_errors(1:3*100, 1:3*100 * 0.05) format(x) format(x, digits=2) +format(x, digits=2, decimals=TRUE) format(x, scientific=TRUE) format(x, notation="plus-minus") diff --git a/tests/testthat/test-print.R b/tests/testthat/test-print.R index 7a2a947..a966e00 100644 --- a/tests/testthat/test-print.R +++ b/tests/testthat/test-print.R @@ -23,6 +23,9 @@ test_that("error formatting works properly", { expect_equal(format(x, notation="parenthesis", scientific=TRUE), c("1(1000)e4", "1.1(1)e4", "1.111(1)e4", "1.1111(1)e4", "1.11112(1)e4", "1.111122(1)e4", "1.111122222(1)e4", "1.111122222000(1)e4")) + expect_equal(format(x, notation="parenthesis", digits=3, decimals=TRUE), + c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)", + "11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)")) expect_equal(format(x, notation="plus-minus"), sapply(list( c("10000", "10000000"), c("11000", "1000"), c("11110", "10"), c("11111", "1"), @@ -41,14 +44,6 @@ test_that("error formatting works properly", { c("(1.11112", "0.00001)e4"), c("(1.111122", "0.000001)e4"), c("(1.111122222", "0.000000001)e4"), c("(1.111122222000", "0.000000000001)e4")), paste, collapse=paste("", .pm, ""))) - # - # test using option to keep decimal point in uncertainty in parenthesis notation - # - saved_options = options(errors.parenthesis.unc.dec.point = TRUE) - expect_equal(format(x, notation="parenthesis", digits=3), - c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)", - "11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)")) - options(saved_options) x <- set_errors(rep(0.827, 3), c(0.119, 0.367, 0.962)) expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(