Skip to content

Commit dbc92e9

Browse files
authored
Merge pull request #2106 from rstudio/fix-integer64
fix: allow numeric formatters to work with `integer64` values
2 parents 336a5f9 + cf3e5c1 commit dbc92e9

File tree

10 files changed

+465
-38
lines changed

10 files changed

+465
-38
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ tests/testthat/_snaps/image.md
122122

123123
tests/testthat/test-info_tables.R
124124
tests/testthat/test-input_data_validation.R
125+
tests/testthat/test-integer64.R
125126

126127
tests/testthat/test-l_fmt_date_time.R
127128

R/fmt.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,11 @@ num_fmt_factory <- function(
14451445

14461446
function(x) {
14471447

1448+
# Convert bit64::integer64 to numeric for formatting
1449+
if (inherits(x, "integer64")) {
1450+
x <- as.numeric(x)
1451+
}
1452+
14481453
# Create `x_str` with the same length as `x`
14491454
x_str <- rep_len(NA_character_, length(x))
14501455

R/fmt_number.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@
237237
#'
238238
#' @section Compatibility of formatting function with data values:
239239
#'
240-
#' `fmt_number()` is compatible with body cells that are of the `"numeric"` or
241-
#' `"integer"` types. Any other types of body cells are ignored during
242-
#' formatting. This is to say that cells of incompatible data types may be
243-
#' targeted, but there will be no attempt to format them.
240+
#' `fmt_number()` is compatible with body cells that are of the `"numeric"`,
241+
#' `"integer"`, or `"integer64"` types. Any other types of body cells are
242+
#' ignored during formatting. This is to say that cells of incompatible data
243+
#' types may be targeted, but there will be no attempt to format them.
244244
#'
245245
#' @section Compatibility of arguments with the `from_column()` helper function:
246246
#'
@@ -540,7 +540,8 @@ fmt_number <- function(
540540
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
541541
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system)
542542

543-
valid_class <- c("numeric", "integer")
543+
valid_class <- c("numeric", "integer", "integer64")
544+
544545
check_columns_valid_if_strict(data, {{ columns }}, valid_class, call = fn_call)
545546

546547
# Set the `formatC_format` option according to whether number
@@ -685,10 +686,10 @@ fmt_number <- function(
685686
#'
686687
#' @section Compatibility of formatting function with data values:
687688
#'
688-
#' `fmt_integer()` is compatible with body cells that are of the `"numeric"` or
689-
#' `"integer"` types. Any other types of body cells are ignored during
690-
#' formatting. This is to say that cells of incompatible data types may be
691-
#' targeted, but there will be no attempt to format them.
689+
#' `fmt_integer()` is compatible with body cells that are of the `"numeric"`,
690+
#' `"integer"`, or `"integer64"` types. Any other types of body cells are
691+
#' ignored during formatting. This is to say that cells of incompatible data
692+
#' types may be targeted, but there will be no attempt to format them.
692693
#'
693694
#' @section Compatibility of arguments with the `from_column()` helper function:
694695
#'

R/format_data.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ fmt_scientific <- function(
368368
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
369369
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system = "intl")
370370

371-
valid_class <- c("numeric", "integer")
371+
valid_class <- c("numeric", "integer", "integer64")
372372
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
373373

374374
# If `n_sigfig` is defined (and not `NA`) modify the number of
@@ -898,7 +898,7 @@ fmt_engineering <- function(
898898
# of suffix labels, or NULL (the case where `suffixing` is FALSE)
899899
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by, system = "intl")
900900

901-
valid_class <- c("numeric", "integer")
901+
valid_class <- c("numeric", "integer", "integer64")
902902
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
903903

904904
# Pass `data`, `columns`, `rows`, and the formatting
@@ -1518,7 +1518,7 @@ fmt_number_si <- function(
15181518
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
15191519
dec_mark <- get_locale_dec_mark(locale, dec_mark)
15201520

1521-
valid_class <- c("numeric", "integer")
1521+
valid_class <- c("numeric", "integer", "integer64")
15221522
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
15231523

15241524
# Pass `data`, `columns`, `rows`, and the formatting
@@ -2116,7 +2116,7 @@ fmt_percent <- function(
21162116
locale <- normalize_locale(locale = locale)
21172117
locale <- resolve_locale(data = data, locale = locale)
21182118

2119-
valid_class <- c("numeric", "integer")
2119+
valid_class <- c("numeric", "integer", "integer64")
21202120
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
21212121

21222122
if (scale_values) {
@@ -2426,7 +2426,7 @@ fmt_partsper <- function(
24262426
locale <- normalize_locale(locale = locale)
24272427
locale <- resolve_locale(data = data, locale = locale)
24282428

2429-
valid_class <- c("numeric", "integer")
2429+
valid_class <- c("numeric", "integer", "integer64")
24302430
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
24312431

24322432
# Scale values according to `to_units` value
@@ -2801,7 +2801,7 @@ fmt_fraction <- function(
28012801
))
28022802
}
28032803

2804-
valid_class <- c("numeric", "integer")
2804+
valid_class <- c("numeric", "integer", "integer64")
28052805
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
28062806

28072807
# Use locale-based `sep_mark` if a locale ID is provided
@@ -3200,10 +3200,10 @@ round_gt <- function(x, digits = 0) {
32003200
#'
32013201
#' @section Compatibility of formatting function with data values:
32023202
#'
3203-
#' `fmt_currency()` is compatible with body cells that are of the `"numeric"` or
3204-
#' `"integer"` types. Any other types of body cells are ignored during
3205-
#' formatting. This is to say that cells of incompatible data types may be
3206-
#' targeted, but there will be no attempt to format them.
3203+
#' `fmt_currency()` is compatible with body cells that are of the `"numeric"`,
3204+
#' `"integer"`, or `"integer64"` types. Any other types of body cells are
3205+
#' ignored during formatting. This is to say that cells of incompatible data
3206+
#' types may be targeted, but there will be no attempt to format them.
32073207
#'
32083208
#' @section Compatibility of arguments with the `from_column()` helper function:
32093209
#'
@@ -3522,7 +3522,7 @@ fmt_currency <- function(
35223522
locale <- normalize_locale(locale = locale)
35233523
locale <- resolve_locale(data = data, locale = locale)
35243524

3525-
valid_class <- c("numeric", "integer")
3525+
valid_class <- c("numeric", "integer", "integer64")
35263526
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
35273527

35283528
# Resolve the currency either from direct input in `currency` or
@@ -3731,7 +3731,7 @@ fmt_roman <- function(
37313731
# Ensure that arguments are matched
37323732
case <- rlang::arg_match0(case, values = c("upper", "lower"))
37333733

3734-
valid_class <- c("numeric", "integer")
3734+
valid_class <- c("numeric", "integer", "integer64")
37353735
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
37363736

37373737
# Pass `data`, `columns`, `rows`, and the formatting
@@ -3969,7 +3969,7 @@ fmt_index <- function(
39693969
# Use locale-based `idx_set` if a locale ID is provided
39703970
idx_set <- get_locale_idx_set(locale)
39713971

3972-
valid_class <- c("numeric", "integer")
3972+
valid_class <- c("numeric", "integer", "integer64")
39733973
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
39743974

39753975
# Pass `data`, `columns`, `rows`, and the formatting
@@ -4338,7 +4338,7 @@ fmt_spelled_num <- function(
43384338
# Obtain a locale-based `num_spellout_set` vector
43394339
num_spellout_set <- get_locale_num_spellout(locale = locale)
43404340

4341-
valid_class <- c("numeric", "integer")
4341+
valid_class <- c("numeric", "integer", "integer64")
43424342
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
43434343

43444344
# Pass `data`, `columns`, `rows`, and the formatting
@@ -4650,7 +4650,7 @@ fmt_bytes <- function(
46504650
locale <- normalize_locale(locale = locale)
46514651
locale <- resolve_locale(data = data, locale = locale)
46524652

4653-
valid_class <- c("numeric", "integer")
4653+
valid_class <- c("numeric", "integer", "integer64")
46544654
check_columns_valid_if_strict(data, {{ columns }}, valid_class)
46554655

46564656
# Use locale-based marks if a locale ID is provided

R/utils.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2437,7 +2437,17 @@ column_classes_are_valid <- function(data, columns, valid_classes, call = rlang:
24372437
FUN.VALUE = logical(1),
24382438
USE.NAMES = FALSE,
24392439
# TRUE if inherits any of the valid classes
2440-
FUN = function(x) inherits(x, valid_classes)
2440+
FUN = function(x) {
2441+
# Check standard inheritance
2442+
if (inherits(x, valid_classes)) {
2443+
return(TRUE)
2444+
}
2445+
# If valid_classes includes numeric or integer types, also check for bit64::integer64
2446+
if (any(c("numeric", "integer") %in% valid_classes) && inherits(x, "integer64")) {
2447+
return(TRUE)
2448+
}
2449+
FALSE
2450+
}
24412451
)
24422452
)
24432453
}

R/utils_render_common.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,19 @@ is_compatible_formatter <- function(table, column, rows, compat) {
132132
return(TRUE)
133133
}
134134

135-
inherits(table[[column]][rows], compat)
135+
column_data <- table[[column]][rows]
136+
137+
# Check for standard class inheritance
138+
if (inherits(column_data, compat)) {
139+
return(TRUE)
140+
}
141+
142+
# If compat includes numeric or integer types, also check for bit64::integer64
143+
if (any(c("numeric", "integer") %in% compat) && inherits(column_data, "integer64")) {
144+
return(TRUE)
145+
}
146+
147+
FALSE
136148
}
137149

138150
#' Render any formatting directives available in the `substitutions` list

man/fmt_currency.Rd

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

man/fmt_integer.Rd

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

man/fmt_number.Rd

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

0 commit comments

Comments
 (0)