Skip to content

Commit f6bc663

Browse files
committed
some refactoring, redoc, update NEWS, bump version
1 parent fdcc671 commit f6bc663

File tree

7 files changed

+92
-47
lines changed

7 files changed

+92
-47
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: errors
22
Type: Package
33
Title: Uncertainty Propagation for R Vectors
4-
Version: 0.4.1
4+
Version: 0.4.2
55
Authors@R: c(
66
person("Iñaki", "Ucar", email="[email protected]",
77
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")),
@@ -20,6 +20,6 @@ LazyData: true
2020
Depends: R (>= 3.0.0)
2121
Suggests: dplyr (>= 1.0.0), vctrs (>= 0.5.0), pillar, ggplot2 (>= 3.4.0),
2222
testthat, vdiffr, knitr, rmarkdown
23-
RoxygenNote: 7.2.3
23+
RoxygenNote: 7.3.2
2424
Roxygen: list(old_usage = TRUE)
2525
VignetteBuilder: knitr

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# errors 0.4.2
2+
3+
- Add support for PDG rounding rules (@davidchall #59 addressing #45).
4+
15
# errors 0.4.1
26

37
- Switch from `size` (deprecated in `ggplot2` v3.4.0) to `linewidth` aesthetic

R/errors.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#' Measurement Errors in \R. \emph{The R Journal}, 10(2), 549-557.
3535
#' \doi{10.32614/RJ-2018-075}
3636
#'
37-
#' @docType package
3837
#' @import stats
3938
#' @name errors-package
4039
#'
@@ -77,7 +76,7 @@
7776
#' # Predicted correction for 30 degC
7877
#' (b.30 <- y1 + y2 * set_errors(30 - 20))
7978
#'
80-
NULL
79+
"_PACKAGE"
8180

8281
#' Handle Uncertainty on a Numeric Vector
8382
#'

R/print.R

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#' @param x an \code{errors} object.
66
#' @param digits how many significant digits are to be used for uncertainties.
77
#' The default, \code{NULL}, uses \code{getOption("errors.digits", 1)}.
8-
#' Use `digits="pdg"` to choose an appropriate number of digits for each value
9-
#' according to the Particle Data Group rounding rule.
8+
#' Use \code{digits="pdg"} to choose an appropriate number of digits for each
9+
#' value according to the Particle Data Group rounding rule (see references).
1010
#' @param scientific logical specifying whether the elements should be
1111
#' encoded in scientific format.
1212
#' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"}
@@ -34,25 +34,28 @@ format.errors = function(x,
3434
...)
3535
{
3636
stopifnot(notation %in% c("parenthesis", "plus-minus"))
37+
3738
if (is.null(digits))
38-
digits = getOption("errors.digits", 1)
39+
digits <- getOption("errors.digits", 1)
40+
digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x))
41+
3942
scipen <- getOption("scipen", 0)
4043
prepend <- rep("", length(x))
4144
append <- rep("", length(x))
4245

43-
if (digits == "pdg")
44-
digits <- digits_pdg(.e(x))
45-
4646
e <- signif(.e(x), digits)
47-
exponent <- get_exponent(x)
47+
nulle <- e == 0 & !is.na(e)
48+
xexp <- get_exponent(x)
4849
value_digits <- ifelse(e, digits - get_exponent(e), digits)
49-
value <- ifelse(e, signif(.v(x), exponent + value_digits), .v(x))
50+
value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x))
51+
value <- ifelse(is.finite(value), value, .v(x))
5052

51-
cond <- (scientific | (exponent > 4+scipen | exponent < -3-scipen)) & is.finite(e)
52-
e[cond] <- e[cond] * 10^(-exponent[cond])
53-
value[cond] <- value[cond] * 10^(-exponent[cond])
54-
value_digits[cond] <- digits - get_exponent(e)[cond]
55-
value_digits[is.infinite(value_digits)] <- 0
53+
cond <- (scientific | (xexp > 4+scipen | xexp < -3-scipen)) & is.finite(e)
54+
e[cond] <- e[cond] * 10^(-xexp[cond])
55+
value[cond] <- value[cond] * 10^(-xexp[cond])
56+
value_digits[cond] <- digits[cond] - get_exponent(e)[cond]
57+
value_digits[!is.finite(value_digits)] <- 0
58+
value_digits[nulle] <- getOption("digits", 7)
5659

5760
if (notation == "parenthesis") {
5861
sep <- "("
@@ -63,23 +66,22 @@ format.errors = function(x,
6366
prepend[cond] <- "("
6467
append[cond] <- ")"
6568
}
66-
append[cond] <- paste(append[cond], "e", exponent[cond], sep="")
69+
append[cond] <- paste(append[cond], "e", xexp[cond], sep="")
6770

6871
value <- sapply(seq_along(value), function(i) {
69-
if (!is.finite(e[[i]]))
70-
format(.v(x)[[i]])
71-
else if (e[[i]])
72-
formatC(value[[i]], format="f", digits=max(0, value_digits[[i]]-1), decimal.mark=getOption("OutDec"))
73-
else format(value[[i]])
72+
formatC(value[[i]], format="f",
73+
digits=max(0, value_digits[[i]]-1),
74+
decimal.mark=getOption("OutDec"))
75+
})
76+
value[nulle] <- prettyNum(value[nulle], drop0trailing=TRUE)
77+
78+
e <- sapply(seq_along(digits), function(i) {
79+
formatC(e[[i]], format="fg", flag="#",
80+
digits=digits[[i]], width=max(1, digits[[i]]),
81+
decimal.mark=getOption("OutDec"))
7482
})
75-
e <- if (length(unique(digits)) > 1) {
76-
sapply(seq_along(digits), function(i) {
77-
formatC(e[[i]], format="fg", flag="#", digits=digits[[i]], width=max(1, digits[[i]]), decimal.mark=getOption("OutDec"))
78-
})
79-
} else {
80-
formatC(e, format="fg", flag="#", digits=digits[[1]], width=max(1, digits[[1]]), decimal.mark=getOption("OutDec"))
81-
}
8283
e <- sub("\\.$", "", e)
84+
8385
paste(prepend, value, sep, e, append, sep="")
8486
}
8587

man/format.errors.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_errors.Rd

Lines changed: 53 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-print.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ test_that("error formatting works properly", {
5151
expect_equal(format(x - set_errors(10)), "0(1)")
5252
expect_equal(format(x - x), "0(0)")
5353

54-
x <- set_errors(c(0.4, NA, NaN, Inf))
54+
x <- set_errors(c(0.4, NA, NaN, Inf, -Inf))
5555
expect_equal(format(x[1]), "0.4(0)")
5656
expect_equal(format(x[2]), "NA(NA)")
5757
expect_equal(format(x[3]), "NaN(NaN)")
5858
expect_equal(format(x[4]), "Inf(Inf)")
59+
expect_equal(format(x[5]), "-Inf(Inf)")
5960
})

0 commit comments

Comments
 (0)