Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt_currency silently fails on bit64::integer64 values #1932

Open
2 tasks done
npelikan opened this issue Dec 17, 2024 · 1 comment
Open
2 tasks done

fmt_currency silently fails on bit64::integer64 values #1932

npelikan opened this issue Dec 17, 2024 · 1 comment
Assignees

Comments

@npelikan
Copy link

npelikan commented Dec 17, 2024

Prework

Description

When a bit64::integer64 column is provided to fmt_currency(), no formatting occurs, and no warnings are displayed. This is relevant as queries to many databases via the odbc package return integer64 columns when a database column type calls for a large integer (ie Databricks' bigint type).

Reproducible example

df <- tibble::tibble(
  name = c("foo", "bar"),
  money = bit64::as.integer64(c(1000, 2000))
)

# fails
df |>
  gt::gt() |>
  gt::fmt_currency(columns = money, currency = "USD")

# works
df$money <- as.numeric(df$money)
df |>
  gt::gt() |>
  gt::fmt_currency(columns = money, currency = "USD")

Expected result

Either formatting to work successfully, or a warning displayed that the indicated column cannot be formatted due to a type mismatch.

Session info

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.37     utf8_1.2.4        R6_2.5.1          fastmap_1.2.0    
 [5] bit_4.5.0.1       tidyselect_1.2.1  magrittr_2.0.3    glue_1.8.0       
 [9] tibble_3.2.1      htmltools_0.5.8.1 pkgconfig_2.0.3   gt_0.11.1        
[13] bit64_4.5.2       dplyr_1.1.4       generics_0.1.3    lifecycle_1.0.4  
[17] xml2_1.3.6        cli_3.6.3         fansi_1.0.6       sass_0.4.9       
[21] vctrs_0.6.5       withr_3.0.2       renv_1.0.7        compiler_4.4.0   
[25] tools_4.4.0       pillar_1.9.0      rlang_1.1.4  
@olivroy
Copy link
Collaborator

olivroy commented Jan 7, 2025

Hi! Thanks for the report. The reason is that fmt_currency() (and most other fmt_*() functions only work with numeric or integer types. From fmt_currency() docs

fmt_currency() is compatible with body cells that are of the "numeric" or "integer" types. Any other types of body cells are ignored during formatting. This is to say that cells of incompatible data types may be targeted, but there will be no attempt to format them.

You'd likely need to use fmt() to format these.

You can set an option to be on the safe side, but that means you will have to manually specify columns in all your fmt_*() calls.

options(gt.strict_column_fmt = TRUE)
df <- tibble::tibble(
    name = c("foo", "bar"),
    money = bit64::as.integer64(c(1000, 2000))
)

df |>
    gt::gt() |>
    gt::fmt(columns = money, compat = "integer64", fns =   function(x) paste0("$", x, "USD"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants