| 1 |
#' Format a numeric vector in a tibble |
| 2 |
#' |
| 3 |
#' These functions are reexported as [tibble::num()] and [tibble::set_num_opts()]. |
| 4 |
#' |
| 5 |
#' @keywords internal |
| 6 |
#' @export |
| 7 |
num <- function(x, ..., |
| 8 |
sigfig = NULL, digits = NULL, |
| 9 |
label = NULL, scale = NULL, |
| 10 |
notation = c("fit", "dec", "sci", "eng", "si"), |
| 11 |
fixed_exponent = NULL, |
| 12 |
extra_sigfig = NULL) { |
| 13 |
stopifnot(is.numeric(x)) |
| 14 |
stopifnot(is.null(digits) || is_integerish(digits)) |
| 15 |
check_dots_empty() |
| 16 |
|
| 17 |
# FIXME: math and arith should also work for integers |
| 18 |
x[] <- as.numeric(x) |
| 19 |
|
| 20 |
if (missing(notation)) { |
| 21 |
notation <- NULL |
| 22 |
} |
| 23 |
|
| 24 |
# FIXME: new_vctr() overrides class attribute, doesn't support subclassing |
| 25 |
out <- set_num_opts( |
| 26 |
x, |
| 27 |
sigfig = sigfig, |
| 28 |
digits = digits, |
| 29 |
label = label, |
| 30 |
scale = scale, |
| 31 |
notation = notation, |
| 32 |
fixed_exponent = fixed_exponent, |
| 33 |
extra_sigfig = extra_sigfig |
| 34 |
) |
| 35 |
|
| 36 |
# FIXME: Include class(x) to support subclassing/mixin? |
| 37 |
# Needs vec_base(): vec_data() but only strips vctrs_vctr class? |
| 38 |
# Avoid inheriting from numeric (?): https://github.com/r-lib/vctrs/issues/1339 |
| 39 |
new_class <- c("pillar_num", "pillar_vctr", "vctrs_vctr", "double") |
| 40 |
class(out) <- new_class |
| 41 |
|
| 42 |
out |
| 43 |
} |
| 44 |
|
| 45 |
#' @export |
| 46 |
vec_ptype_full.pillar_num <- function(x, ...) { |
| 47 |
# No longer inherited in vctrs > 0.4.1, |
| 48 |
# https://github.com/r-lib/vctrs/pull/1571 |
| 49 |
format(attr(x, "pillar", exact = TRUE)) |
| 50 |
} |
| 51 |
|
| 52 |
#' @export |
| 53 |
vec_ptype_abbr.pillar_num <- function(x, ...) { |
| 54 |
pillar_attr <- attr(x, "pillar", exact = TRUE) |
| 55 |
notation <- pillar_attr$notation |
| 56 |
if (is.null(notation)) { |
| 57 |
notation <- "num" |
| 58 |
} |
| 59 |
|
| 60 |
sigfig <- pillar_attr$sigfig |
| 61 |
digits <- pillar_attr$digits |
| 62 |
|
| 63 |
if (!is.null(digits)) { |
| 64 |
if (digits > 0) { |
| 65 |
out <- paste0(notation, ":.", digits, "!") |
| 66 |
} else { |
| 67 |
out <- paste0(notation, ":.", -digits) |
| 68 |
} |
| 69 |
} else if (!is.null(sigfig)) { |
| 70 |
out <- paste0(notation, ":", sigfig) |
| 71 |
} else { |
| 72 |
out <- notation |
| 73 |
} |
| 74 |
|
| 75 |
out |
| 76 |
} |
| 77 |
|
| 78 |
#' @export |
| 79 |
format.pillar_num <- function(x, trim = FALSE, ...) { |
| 80 |
"!!!!DEBUG format.pillar_num()" |
| 81 |
|
| 82 |
shaft <- pillar_shaft(x) |
| 83 |
out <- format(shaft, width = get_width(shaft)) |
| 84 |
if (trim) { |
| 85 |
attributes(out) <- NULL |
| 86 |
} else { |
| 87 |
out <- format(out, align = "right") |
| 88 |
} |
| 89 |
out |
| 90 |
} |
| 91 |
|
| 92 |
#' @method vec_arith pillar_num |
| 93 |
#' @export |
| 94 |
vec_arith.pillar_num <- function(op, x, y, ...) { |
| 95 |
"!!!!DEBUG vec_arith.pillar_num(`v(op)`)" |
| 96 |
|
| 97 |
UseMethod("vec_arith.pillar_num", y) |
| 98 |
} |
| 99 |
#' @method vec_arith.pillar_num default |
| 100 |
#' @export |
| 101 |
vec_arith.pillar_num.default <- function(op, x, y, ...) { |
| 102 |
"!!!!DEBUG vec_arith.pillar_num.default(`v(op)`)" |
| 103 |
out <- vec_arith_base(op, vec_proxy(x), vec_proxy(y)) |
| 104 |
|
| 105 |
if (inherits(x, "pillar_num")) { |
| 106 |
vec_restore(out, x) |
| 107 |
} else { |
| 108 |
vec_restore(out, y) |
| 109 |
} |
| 110 |
} |
| 111 |
#' @method vec_arith.pillar_num MISSING |
| 112 |
#' @export |
| 113 |
vec_arith.pillar_num.MISSING <- function(op, x, y, ...) { |
| 114 |
"!!!!DEBUG vec_arith.pillar_num.MISSING(`v(op)`)" |
| 115 |
# FIXME |
| 116 |
out <- vec_arith_base(op, 0, vec_proxy(x)) |
| 117 |
|
| 118 |
vec_restore(out, x) |
| 119 |
} |
| 120 |
|
| 121 |
#' @method vec_arith.numeric pillar_num |
| 122 |
#' @export |
| 123 |
vec_arith.numeric.pillar_num <- vec_arith.pillar_num.default |
| 124 |
|
| 125 |
|
| 126 |
#' @export |
| 127 |
vec_math.pillar_num <- function(.fn, .x, ...) { |
| 128 |
"!!!!DEBUG vec_math(`v(.fn)`)" |
| 129 |
|
| 130 |
out <- vec_math_base(.fn, vec_proxy(.x), ...) |
| 131 |
|
| 132 |
if (is.numeric(out)) { |
| 133 |
out <- vec_restore(out, .x) |
| 134 |
} |
| 135 |
|
| 136 |
out |
| 137 |
} |
| 138 |
|
| 139 |
#' @export |
| 140 |
#' @rdname num |
| 141 |
set_num_opts <- function(x, ..., |
| 142 |
sigfig = NULL, digits = NULL, |
| 143 |
label = NULL, scale = NULL, |
| 144 |
notation = c("fit", "dec", "sci", "eng", "si"), |
| 145 |
fixed_exponent = NULL, |
| 146 |
extra_sigfig = NULL) { |
| 147 |
check_dots_empty() |
| 148 |
|
| 149 |
if (missing(notation)) { |
| 150 |
notation <- NULL |
| 151 |
} else if (!is.null(notation)) { |
| 152 |
notation <- arg_match(notation) |
| 153 |
} |
| 154 |
|
| 155 |
if (!is.null(fixed_exponent) && !is.null(notation)) { |
| 156 |
if (fixed_exponent && notation == "dec") { |
| 157 |
abort('Incomatible arguments: `notation = "dec"` and `fixed_exponent` given') |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if (!is.null(scale) && is.null(label)) { |
| 162 |
abort("Must set `label` if `scale` is provided.") |
| 163 |
} |
| 164 |
|
| 165 |
if (!is.null(digits) && !is.null(extra_sigfig)) { |
| 166 |
abort("Incompatible arguments: `extra_sigfig` and `digits`.") |
| 167 |
} |
| 168 |
|
| 169 |
pillar_attr <- structure( |
| 170 |
list( |
| 171 |
sigfig = sigfig, |
| 172 |
digits = digits, |
| 173 |
label = label, |
| 174 |
scale = scale, |
| 175 |
notation = notation, |
| 176 |
fixed_exponent = fixed_exponent, |
| 177 |
extra_sigfig = extra_sigfig |
| 178 |
), |
| 179 |
class = c("pillar_num_attr", "pillar_vctr_attr", "tibble_vec_attr") |
| 180 |
) |
| 181 |
attr(x, "pillar") <- pillar_attr |
| 182 |
x |
| 183 |
} |
| 184 |
|
| 185 |
#' @export |
| 186 |
format.pillar_num_attr <- function(x, ...) { |
| 187 |
notation <- x$notation |
| 188 |
if (is.null(notation)) { |
| 189 |
class <- "pillar_num" |
| 190 |
} else { |
| 191 |
class <- paste0("pillar_num(", notation, ")") |
| 192 |
} |
| 193 |
|
| 194 |
sigfig <- x$sigfig |
| 195 |
digits <- x$digits |
| 196 |
label <- x$label |
| 197 |
extra_sigfig <- x$extra_sigfig |
| 198 |
|
| 199 |
if (!is.null(digits)) { |
| 200 |
if (digits >= 0) { |
| 201 |
out <- paste0(class, ":.", digits, "!") |
| 202 |
} else { |
| 203 |
out <- paste0(class, ":.", -digits) |
| 204 |
} |
| 205 |
} else if (!is.null(sigfig)) { |
| 206 |
out <- paste0(class, ":", sigfig) |
| 207 |
|
| 208 |
if (isTRUE(extra_sigfig)) { |
| 209 |
out <- paste0(out, "*") |
| 210 |
} |
| 211 |
} else if (isTRUE(extra_sigfig)) { |
| 212 |
out <- paste0(class, "*") |
| 213 |
} else { |
| 214 |
out <- class |
| 215 |
} |
| 216 |
|
| 217 |
if (!is.null(label)) { |
| 218 |
out <- paste0(out, "{", label, "}") |
| 219 |
} |
| 220 |
|
| 221 |
scale <- x$scale |
| 222 |
|
| 223 |
if (!is.null(scale)) { |
| 224 |
out <- paste0(out, "*", scale) |
| 225 |
} |
| 226 |
|
| 227 |
fixed_exponent <- x$fixed_exponent |
| 228 |
|
| 229 |
if (!is.null(fixed_exponent)) { |
| 230 |
out <- paste0(out, "|", fixed_exponent) |
| 231 |
} |
| 232 |
|
| 233 |
out |
| 234 |
} |
| 235 |
|
| 236 |
#' @export |
| 237 |
vec_ptype2.pillar_num.pillar_num <- function(x, y, ...) { |
| 238 |
x |
| 239 |
} |
| 240 |
#' @export |
| 241 |
vec_ptype2.pillar_num.double <- function(x, y, ...) { |
| 242 |
x |
| 243 |
} |
| 244 |
#' @export |
| 245 |
vec_ptype2.double.pillar_num <- function(x, y, ...) { |
| 246 |
y |
| 247 |
} |
| 248 |
|
| 249 |
#' @export |
| 250 |
vec_ptype2.pillar_num.integer <- function(x, y, ...) { |
| 251 |
x |
| 252 |
} |
| 253 |
#' @export |
| 254 |
vec_ptype2.integer.pillar_num <- function(x, y, ...) { |
| 255 |
y |
| 256 |
} |
| 257 |
|
| 258 |
#' @export |
| 259 |
vec_cast.pillar_num.pillar_num <- function(x, to, ...) { |
| 260 |
pillar_x <- attr(x, "pillar", exact = TRUE) |
| 261 |
pillar_to <- attr(to, "pillar", exact = TRUE) |
| 262 |
|
| 263 |
pillar_x_label <- pillar_x$label |
| 264 |
pillar_to_label <- pillar_to$label |
| 265 |
|
| 266 |
if (!is.null(pillar_x_label) && !is.null(pillar_to_label)) { |
| 267 |
if (!identical(pillar_x$label, pillar_to$label)) { |
| 268 |
abort("Only `pillar_num` objects with the same label can be combined.") |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
attr(x, "pillar") <- pillar_to |
| 273 |
x |
| 274 |
} |
| 275 |
#' @export |
| 276 |
vec_cast.double.pillar_num <- function(x, to, ...) { |
| 277 |
vec_data(x) |
| 278 |
} |
| 279 |
#' @export |
| 280 |
vec_cast.pillar_num.double <- function(x, to, ...) { |
| 281 |
out <- num(x) |
| 282 |
attr(out, "pillar") <- attr(to, "pillar") |
| 283 |
out |
| 284 |
} |
| 285 |
#' @export |
| 286 |
vec_cast.pillar_num.integer <- function(x, to, ...) { |
| 287 |
out <- num(as.numeric(x)) |
| 288 |
attr(out, "pillar") <- attr(to, "pillar") |
| 289 |
out |
| 290 |
} |