rstudio / pins-r

Pin, discover, and share resources
https://pins.rstudio.com
Other
312 stars 63 forks source link

json doesn't preserve date class #687

Closed rjake closed 1 year ago

rjake commented 1 year ago

Not sure if this is expected but using the json format doesn't preserve dates as dates. They come through as characters. If this is expected, it might help others to add this caveat to the argument description.


library(pins)
board <- board_rsconnect(...)

ggplot2::economics |> 
  pin_write(
    board = board, 
    name = "rjake/test",
    type = "json"
  )

pin_read(board, "rjake/test") |> 
  str()

# 'data.frame': 574 obs. of  6 variables:
 $ date    : chr  "1967-07-01"   ...    <---- here
 $ pce     : num  507 510 516    ...
 $ pop     : num  198712 198911  ...
 $ psavert : num  12.6 12.6 11.9 ...
 $ uempmed : num  4.5 4.7 4.6    ...
 $ unemploy: int  2944 2945 2958 ...
juliasilge commented 1 year ago

This is how JSON itself works; it is a plain text format:

library(jsonlite)

tmp_file <- tempfile()
write_json(ggplot2::economics, tmp_file)
out <- read_json(tmp_file, simplifyVector = TRUE) 
str(out)
#> 'data.frame':    574 obs. of  6 variables:
#>  $ date    : chr  "1967-07-01" "1967-08-01" "1967-09-01" "1967-10-01" ...
#>  $ pce     : num  507 510 516 512 517 ...
#>  $ pop     : num  198712 198911 199113 199311 199498 ...
#>  $ psavert : num  12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
#>  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
#>  $ unemploy: int  2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...

Created on 2022-12-15 with reprex v2.0.2

CSV is a plain text format too, actually:

library(pins)
b <- board_temp()
b %>% pin_write(ggplot2::economics, name = "this-has-dates", type = "csv")
#> Creating new version '20221215T185712Z-ab538'
#> Writing to pin 'this-has-dates'
b %>% pin_read("this-has-dates") %>% str()
#> 'data.frame':    574 obs. of  6 variables:
#>  $ date    : chr  "1967-07-01" "1967-08-01" "1967-09-01" "1967-10-01" ...
#>  $ pce     : num  507 510 516 512 517 ...
#>  $ pop     : num  198712 198911 199113 199311 199498 ...
#>  $ psavert : num  12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
#>  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
#>  $ unemploy: int  2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...

Created on 2022-12-15 with reprex v2.0.2

Both RDS and Arrow formats are binary, so they let you store non-plain-text R types like dates and factors. I agree with you that it's a good idea to add a bit of a reminder about this in the docs!

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.