|
12 | 12 |
|
13 | 13 | #' @importFrom stats setNames |
14 | 14 | #' @export |
15 | | -get_precision <- function(x, ...){ |
16 | | - # from [Broman::get_precision()] |
17 | | - # a bit of contortion here to control the scipen and digits options and have them returned to their initial values |
18 | | - dots <- list("...") |
19 | | - if(is.null(dots$set_digits) || dots$set_digits) { |
20 | | - scipen <- options("scipen")$scipen |
21 | | - digits <- options("digits")$digits |
22 | | - on.exit(options(scipen=scipen, digits=digits)) |
23 | | - options(scipen=100, digits=21) |
24 | | - } |
| 15 | +get_precision <- function(x, ...) { |
| 16 | + # from [Broman::get_precision()] |
| 17 | + # a bit of contortion here to control the scipen and digits options and have them returned to their initial values |
| 18 | + dots <- list("...") |
| 19 | + # if (is.null(dots$set_digits) || dots$set_digits) { |
| 20 | + # scipen <- options("scipen")$scipen |
| 21 | + # digits <- options("digits")$digits |
| 22 | + # on.exit(options(scipen=scipen, digits=digits)) |
| 23 | + # options(scipen=1, digits=8) |
| 24 | + # } |
25 | 25 |
|
26 | | - if(length(x) > 1) { # deal with vector input |
27 | | - return(setNames(vapply(x, get_precision, 1, set_digits=FALSE), NULL)) |
28 | | - } |
| 26 | + if (length(x) > 1) { # deal with vector input |
| 27 | + return(setNames(vapply(x, get_precision, 1, set_digits=FALSE), NULL)) |
| 28 | + } |
29 | 29 |
|
30 | | - ### here down, x is a single value |
31 | | - if(is.na(x)) return(NA) # NA -> NA |
32 | | - x <- as.character(x) |
33 | | - if(!grepl(".", x, fixed=TRUE)) return(0) |
34 | | - frac <- strsplit(x, ".", fixed=TRUE)[[1]][2] |
35 | | - if(is.na(frac) || nchar(frac)==0) return(0) |
36 | | - digits <- strsplit(frac, "", fixed=TRUE)[[1]] |
37 | | - max(which(digits != "0")) |
| 30 | + ### here down, x is a single value |
| 31 | + if(is.na(x)) return(NA) # NA -> NA |
| 32 | + x <- as.character(x) |
| 33 | + if(!grepl(".", x, fixed=TRUE)) return(0) |
| 34 | + frac <- strsplit(x, ".", fixed=TRUE)[[1]][2] |
| 35 | + if(is.na(frac) || nchar(frac)==0) return(0) |
| 36 | + digits <- strsplit(frac, "", fixed=TRUE)[[1]] |
| 37 | + max(which(digits != "0")) |
38 | 38 | } |
39 | 39 |
|
40 | 40 | probs_to_string <- function(x, prefix = "q") { |
|
0 commit comments