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

Method to display date difference #164

Closed
elong0527 opened this issue Jun 11, 2021 · 1 comment
Closed

Method to display date difference #164

elong0527 opened this issue Jun 11, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@elong0527
Copy link

The package may need a method to handle difftime.

> x <- data.frame(date = as.Date("2021-06-11") - as.Date("2021-03-05"))
> reactable(x)
Error: No method asJSON S3 class: difftime
> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.9 (Maipo)

Matrix products: default
BLAS/LAPACK: /data/app/Rtoolkit-EIM/1.1.0/lib/libopenblas_nehalemp-r0.2.20.so

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

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

other attached packages:
[1] mk7902p012dmc2_0.0.1 reactable_0.2.3      haven_2.3.1          tidyr_1.1.3          dplyr_1.0.5          testthat_3.0.2      

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0  xfun_0.22         remotes_2.2.0     purrr_0.3.4       vctrs_0.3.6       generics_0.1.0    usethis_2.0.1     htmltools_0.5.1.1 yaml_2.2.1        utf8_1.2.1       
[11] rlang_0.4.10      pkgbuild_1.2.0    pillar_1.5.1      glue_1.4.2        withr_2.4.1       reactR_0.4.4      DBI_1.1.1         sessioninfo_1.1.1 lifecycle_1.0.0   devtools_2.3.2   
[21] htmlwidgets_1.5.3 memoise_2.0.0     evaluate_0.14     knitr_1.31        forcats_0.5.1     callr_3.5.1       fastmap_1.1.0     ps_1.6.0          crosstalk_1.1.1   fansi_0.4.2      
[31] Rcpp_1.0.6        readr_1.4.0       cachem_1.0.4      desc_1.3.0        pkgload_1.2.0     debugme_1.1.0     jsonlite_1.7.2    fs_1.5.0          hms_1.0.0         digest_0.6.27    
[41] processx_3.4.5    rprojroot_2.0.2   cli_2.3.1         tools_4.0.1       magrittr_2.0.1    tibble_3.1.0      crayon_1.4.1      pkgconfig_2.0.3   ellipsis_0.3.1    prettyunits_1.1.1
[51] assertthat_0.2.1  rmarkdown_2.7     rstudioapi_0.13   R6_2.5.0          compiler_4.0.1   
@glin
Copy link
Owner

glin commented Jun 13, 2021

Thanks for the report. This is now fixed in the development version on GitHub (ef76018):

  • reactable() now works for data frames with difftime objects and objects with custom classes (#164).

The error comes from the jsonlite package not supporting the conversion of difftime objects to JSON, so the fix was to force serialization of these unsupported objects using jsonlite::toJSON(x, force = TRUE). It's not perfect because the difftime objects are just treated as raw numbers, but I think it's the best we can do in reactable. The better solution would be to add difftime support to jsonlite.

Without this fix, you can work around this issue by manually converting the difftime column to numeric values, then adding the units to the formatting:

library(reactable)

x <- data.frame(date = as.Date("2021-06-11") - as.Date("2021-03-05"))

x$date <- as.numeric(x$date)

reactable(x, columns = list(
  date = colDef(format = colFormat(suffix = " days"))
))

@glin glin closed this as completed Jun 13, 2021
@glin glin added the bug Something isn't working label Jun 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants