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
0 commit comments