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

373 slices_restore will restore Dates and POSIXT classes #432

Merged
merged 27 commits into from
Oct 18, 2023

Conversation

m7pr
Copy link
Contributor

@m7pr m7pr commented Aug 9, 2023

Closes #373
A very small proposition on how we can guess that a character contains a Date a POSIXT time. Up to be discussed as we might want to just use jsonlite::serializeJSON and jsonlite::unserializeJSON

The code

x <- 
  teal_slice(
    dataname = "ADSL", 
    varname = "COUNTRY", 
    selected = as.POSIXct(c("2023-06-29 18:05:32 CEST", "2023-06-29 18:15:32 CEST")), 
    fixed = TRUE
  )
class(as.list(x)$selected)

t <- 
  teal_slice(
    dataname = "ADSL", 
    varname = "TIME", 
    selected = as.Date(c("2023-06-29", "2023-06-30")), 
    fixed = TRUE
  )
class(as.list(t)$selected)

y <- teal_slices(x, t)
slices_store(y, file = 'test1.json')

z <- slices_restore('test1.json')

class(shiny::isolate(z[[1]]$selected))
class(shiny::isolate(z[[2]]$selected))

The code with results

> x <- 
+   teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 18:05:32 CEST", "2023-06-29 18:15:32 CEST")), 
+     fixed = TRUE
+   )
> class(as.list(x)$selected)
[1] "POSIXct" "POSIXt" 
> 
> t <- 
+   teal_slice(
+     dataname = "ADSL", 
+     varname = "TIME", 
+     selected = as.Date(c("2023-06-29", "2023-06-30")), 
+     fixed = TRUE
+   )
> class(as.list(t)$selected)
[1] "Date"
> 
> y <- teal_slices(x, t)
> slices_store(y, file = 'test1.json')
> 
> z <- slices_restore('test1.json')
> 
> class(shiny::isolate(z[[1]]$selected))
[1] "POSIXct" "POSIXt" 
> class(shiny::isolate(z[[2]]$selected))
[1] "Date"

@m7pr m7pr marked this pull request as draft August 9, 2023 10:03
@m7pr m7pr added the core label Aug 9, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2023

badge

Code Coverage Summary

Filename                      Stmts    Miss  Cover    Missing
--------------------------  -------  ------  -------  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
R/calls_combine_by.R              8       0  100.00%
R/choices_labeled.R              49      14  71.43%   22, 33, 38, 48-53, 65, 69-73
R/count_labels.R                 98       0  100.00%
R/filter_panel_api.R             37       2  94.59%   92, 104
R/FilteredData-utils.R          117      17  85.47%   103-108, 201, 223-232
R/FilteredData.R                588     226  61.56%   100-103, 167, 522-530, 599-608, 630, 651-692, 710-713, 729, 770-803, 818-820, 824-830, 856-884, 906-908, 912-914, 917-928, 932-941, 943-969, 987-1057, 1099, 1122-1144
R/FilteredDataset-utils.R        20       1  95.00%   133
R/FilteredDataset.R             179      67  62.57%   50, 149, 198-204, 232-289, 329-331
R/FilteredDatasetDefault.R      121       9  92.56%   69, 131, 141, 145, 228-232
R/FilteredDatasetMAE.R          134      37  72.39%   27, 113-118, 157-162, 166-167, 187-209
R/FilterPanelAPI.R               10       0  100.00%
R/FilterState-utils.R            97       1  98.97%   286
R/FilterState.R                 361      61  83.10%   88, 212, 230-234, 241-242, 256-257, 263-264, 312, 314, 316, 368, 412, 644, 687-712, 723-742, 777-783, 792-798
R/FilterStateChoices.R          338     106  68.64%   302-305, 317, 360, 384-391, 395-412, 441, 456-467, 479-487, 491-520, 541-544, 547-550, 561-582, 595-596, 606
R/FilterStateDate.R             212     129  39.15%   219, 272-430
R/FilterStateDatettime.R        307     199  35.18%   256, 309-541
R/FilterStateEmpty.R             63      41  34.92%   82, 92-97, 111, 125-176
R/FilterStateExpr.R              75      62  17.33%   138-262
R/FilterStateLogical.R          196     144  26.53%   127, 150, 210, 213-399
R/FilterStateRange.R            410     105  74.39%   254, 378, 506-510, 513-523, 526, 538-544, 555-567, 571-581, 585-587, 601-628, 643, 646, 661-678, 713-718, 728-730
R/FilterStates-utils.R           70       9  87.14%   102, 121, 179-185, 207, 234
R/FilterStates.R                364      30  91.76%   76-80, 189, 320-329, 417-420, 463, 548-552, 597, 718-721
R/FilterStatesDF.R                5       0  100.00%
R/FilterStatesMAE.R              10       1  90.00%   39
R/FilterStatesMatrix.R            3       0  100.00%
R/FilterStatesSE.R              211     157  25.59%   34, 69-71, 81-83, 107-114, 122-129, 152-300
R/include_css_js.R                5       5  0.00%    12-16
R/teal_slice-store.R             25       0  100.00%
R/teal_slice.R                  107       2  98.13%   137, 203
R/teal_slices.R                 139       6  95.68%   144-149, 329
R/test_utils.R                   21       0  100.00%
R/utils.R                        49       2  95.92%   101-102
R/variable_types.R               48      33  31.25%   41-46, 56, 69-104
R/zzz.R                          15      15  0.00%    3-46
TOTAL                          4492    1481  67.03%

Diff against main

Filename                Stmts    Miss  Cover
--------------------  -------  ------  --------
R/teal_slice-store.R      +18      -7  +100.00%
R/teal_slice.R             +5       0  +0.09%
TOTAL                     +23      -7  +0.33%

Results for commit: 7f0020c

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

@chlebowa
Copy link
Contributor

chlebowa commented Aug 9, 2023

I am concerned about time zones.

x <- 
  teal_slice(
    dataname = "ADSL", 
    varname = "COUNTRY", 
    selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "BRA"), 
    fixed = TRUE
  )

y <- teal_slices(x)
slices_store(y, file = 'test1.json')

z <- slices_restore('test1.json')

identical(as.list(y, recursive = TRUE), as.list(z, recursive = TRUE))
[1] FALSE

@m7pr
Copy link
Contributor Author

m7pr commented Aug 9, 2023

Indeed tzone attribute is dropped during the character conversion in store. One more addition would be the collapse of tzone argument at the storage phase to the actual string representation, so that it can be restored with timezone <- substr(slice$selected[1], 21, nchar(slice$selected[1])) and passed to as.POSIXct(slice$selected, tz = timezone).

m7pr added 3 commits August 9, 2023 12:55
Merge branch '373_restore_classes@main' of https://github.com/insightsengineering/teal.slice into 373_restore_classes@main

# Conflicts:
#	R/teal_slice-store.R
@m7pr
Copy link
Contributor Author

m7pr commented Aug 9, 2023

checkout last commit, where I tried to unify stored POSIX to UTC. When storing we change the timezone so the display is displayed in UTC, and when restoring we then know we need to set timezone to UTC. The print/format is different after the restorage but the time representation (as number of seconds from 1970-01-01) is the same

x <- 
  teal_slice(
    dataname = "ADSL", 
    varname = "COUNTRY", 
    selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "NZ"), 
    fixed = TRUE
  )

y <- teal_slices(x)
slices_store(y, file = 'test1.json')

z <- slices_restore('test1.json')

identical(
  as.integer(shiny::isolate(z[[1]]$selected)), 
  as.integer(shiny::isolate(x$selected))
)
[1] TRUE

@m7pr
Copy link
Contributor Author

m7pr commented Sep 4, 2023

TODO: review if teal.slice works if we keep factors converted to characters

Copy link
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on this feature!! 💯

I thought on adding some tests. See branch 373_add_tests that merges to this one.

Note: it skips the last 2 tests for now and might be enabled/removed depending on feedback

R/teal_slice-store.R Outdated Show resolved Hide resolved
R/teal_slice-store.R Outdated Show resolved Hide resolved
@m7pr
Copy link
Contributor Author

m7pr commented Sep 13, 2023

Thanks for the branch with new tests. Just merged to this branch. Still need to figure out what happens for factors

@m7pr m7pr marked this pull request as ready for review September 21, 2023 10:59
@m7pr
Copy link
Contributor Author

m7pr commented Sep 21, 2023

Hey @averissimo and @chlebowa the issue for POSIXct and Date classes are fixed with this PR (this closes #373).

Similar issue appears for factors that are not restored properly, but I created a separate issue for that #467 as this is a bigger deal and became out of scope of this quick change

Copy link
Contributor

@averissimo averissimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me 💯

I won't approve right now to wait for @chlebowa feedback as he commented before

I was a bit concern about using all(grepl(..., but after some tests I can see that teal_slices doesn't work well with a mix bag of selected

time_stamps <- Sys.time() + c(-10 * 60 * 60 * 24, -30, 0)
time_stamps <- as.POSIXct(ceiling(as.double(time_stamps)), tz = "UTC")

# Converts Date to POSIXct
teal_slices(
  teal_slice(
    dataname = "ADSL",
    varname = "EOSDTM",
    selected = c(time_stamps, Sys.Date()),
    fixed = TRUE
  )
)

# Converts POSIXct to Date
teal_slices(
  teal_slice(
    dataname = "ADSL",
    varname = "EOSDTM",
    selected = c(Sys.Date(), time_stamps),
    fixed = TRUE
  )
)

# Assumes all as strings
tss <- teal_slices(
  teal_slice(
    dataname = "ADSL",
    varname = "EOSDTM",
    selected = c("A string", time_stamps, Sys.Date()),
    fixed = TRUE
  )
)

# Fails to convert string to Posix.ct (acceptable)
tss <- teal_slices(
  teal_slice(
    dataname = "ADSL",
    varname = "EOSDTM",
    selected = c(time_stamps, Sys.Date(), "A string"),
    fixed = TRUE
  )
)

@m7pr
Copy link
Contributor Author

m7pr commented Sep 21, 2023

@averissimo please bare in mind, that we will store and restore data provided by end app users and app developers and for those it is hard to mix such structure. End app users will have shiny inputs to provided the data, and the app developer will rather start with direct names of levels or even pull names directly from data. I think mixing classes is unlikely to happen

@chlebowa
Copy link
Contributor

@averissimo The coercions you listed are done by c, actually.

@averissimo
Copy link
Contributor

averissimo commented Sep 21, 2023

@chlebowa & @m7pr I'm always a bit pessimistic with weird edge cases 😅

The only semi-realistic scenario would be if the user deliberately wants to break the application.

tss <- teal_slices(
  teal_slice(
    dataname = "ADSL",
    varname = "EOSDTM",
    selected = c(
      "beta 2023-09-11",
      "release candidate 2023-09-21",
      "release 2023-09-21"
    ),
    fixed = TRUE
  )
)

slices_store(tss, slices_path)
tss_restored <- slices_restore(slices_path)
# Error in charToDate(x) :
# character string is not in a standard unambiguous format

@averissimo
Copy link
Contributor

This could be protected if we look for a full regex match, however, I don't think we should add the complexity (and possible new bugs from it).

# ...
        if (!is.null(slice[[field]])) {
          date_partial_regex <- "^[0-9]{4}-[0-9]{2}-[0-9]{2}"
          time_stamp_regex <- paste0(date_partial_regex, "T[0-9]{2}:[0-9]{2}:[0-9]{2}$")
          slice[[field]] <-
            if (all(grepl(paste0(date_partial_regex, "$"), slice[[field]]))) {
              if (all(grepl(time_stamp_regex, slice[[field]]))) {
                as.POSIXct(gsub("T|Z", " ", slice[[field]]), tz = "UTC")
              } else {
                as.Date(slice[[field]])
              }
            } else {
              slice[[field]]
            }
        }
# ...

@m7pr
Copy link
Contributor Author

m7pr commented Oct 17, 2023

Hey @gogonzo and @chlebowa - thanks for the highly productive and fruitful discussion today.
In the end we decided that POSIX*t classes will be stored in their format(usetz = TRUE) representation so that timezone is stored next to a timestamp in a visible way, in stored file and in the print in console.

The final proposition of the usage looks like below:

> x <- teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "EST"), 
+     fixed = TRUE
+ )
> y <- teal_slices(x)
> y
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 EST", "2023-06-29 00:00:02 EST"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> slices_store(y, file = 'store.json')
> z <- slices_restore('store.json')
> z
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 EST", "2023-06-29 00:00:02 EST"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 

TODO

One thing to be fixed (ON TODO LIST) is that some timezones are named differently in format() and in as.POSIXct().

x <- teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "NZ"), 
+     fixed = TRUE
+ )
> y <- teal_slices(x)
> y
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 NZST", "2023-06-29 00:00:02 NZST"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> slices_store(y, file = 'store.json')
> z <- slices_restore('store.json')
Warning messages:
1: In strptime(xx, f, tz = tz) : unknown timezone 'NZST'
2: In as.POSIXct.POSIXlt(x) : unknown timezone 'NZST'
3: In strptime(x, f, tz = tz) : unknown timezone 'NZST'
4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone 'NZST'
> z
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 GMT", "2023-06-29 00:00:02 GMT"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
Warning message:
In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'NZST'

the same happens for CET that gets changed to CEST, where CEST is not recognized by as.POSIXct

Will work on that and will keep you posted.

@chlebowa
Copy link
Contributor

The validity of time zone codes is platform-dependent, I'm afraid.

@m7pr
Copy link
Contributor Author

m7pr commented Oct 17, 2023

lemme investigate :)

@averissimo
Copy link
Contributor

😠 this is an upstream bug from jsonlite as the ISO8601 standard should ensure that only valid timezones are used (none, UTC or UTC offset)

@chlebowa
Copy link
Contributor

Ah, but we are stepping away from ISO8601 and trying to convert to strings by ourselves and store those.

@averissimo
Copy link
Contributor

Oh my bad then. I missed it in the comments as the code doesn't seem to apply that

If you're considering using the format(..., usetz = TRUE) function explicitly, then having a manual format param might be an option to fully control the process (with %z - lowercase - to deal with timezones):

format(Sys.time(), format = "%Y-%m-%d %H:%M:%S %z", usetz = FALSE)
as.POSIXct(c("2023-06-29 17:02:01 +0200"), tryFormats = "%Y-%m-%d %H:%M:%S %z")

@m7pr
Copy link
Contributor Author

m7pr commented Oct 17, 2023

This doesn't appear if we change the class of final restored object from POSIXct to POSIXlt. Assuming we know a timezone exists in the stored file, should we use POSIXlt ? cd218bf

x <- teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), 
+     fixed = TRUE
+ )
> y <- teal_slices(x)
> y
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 CEST", "2023-06-29 00:00:02 CEST"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> slices_store(y, file = 'store.json')
> z <- slices_restore('store.json')
> z
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 CEST", "2023-06-29 00:00:02 CEST"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> class(shiny::isolate(z[[1]]$selected))
[1] "POSIXlt" "POSIXt"

@chlebowa
Copy link
Contributor

%Y-%m-%d %H:%M:%M is the default format and using %Z is discouraged (?strptime).

> now <- Sys.time()
> as.character(now)
[1] "2023-10-17 17:44:45"
> as.character(now, usetz = TRUE)
[1] "2023-10-17 17:44:45 CEST"

@chlebowa
Copy link
Contributor

This doesn't appear if we change the class of final restored object from POSIXct to POSIXlt. Assuming we know a timezone exists in the stored file, should we use POSIXlt ?

🤔

I would rigorously test if POSIXlt will properly set state with FilterState$set_state.

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023 via email

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

> as.POSIXlt("2023-10-08 08:46:40 UTC", format = "%Y-%m-%d %H:%M:%S")
[1] "2023-10-08 08:46:40 CEST"
> as.POSIXlt("2023-10-08 08:46:40 CEST", format = "%Y-%m-%d %H:%M:%S")
[1] "2023-10-08 08:46:40 CEST"

as.POSIXlt on "%Y-%m-%d %H:%M:%S %z" does not pay attention to %z. Even though %z is discouraged in favour of tz parameter, the tz parameter has this disadvantage that it operates on short-codes that tend to be system-specific and daylight-saving-time specific. I rather go with %z and @averissimo proposition

format(Sys.time(), format = "%Y-%m-%d %H:%M:%S %z", usetz = FALSE)
[1] "2023-10-18 10:59:27 +0200"

To have explicit amount of hour displayed after the timestamp. So now we only deal with numbers (and not characters that tend to change. POSIXct changes CET to CEST and then doesn`t understand what CEST is

> format(as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), usetz = TRUE)
[1] "2023-06-29 00:00:01 CEST" "2023-06-29 00:00:02 CEST"
> as.POSIXct(format(as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), usetz = TRUE), tz = 'CEST')
[1] "2023-06-29 00:00:01 GMT" "2023-06-29 00:00:02 GMT"
Warning messages:
1: In strptime(xx, f, tz = tz) : unknown timezone 'CEST'
2: In as.POSIXct.POSIXlt(x) : unknown timezone 'CEST'
3: In strptime(x, f, tz = tz) : unknown timezone 'CEST'
4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone 'CEST'
5: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'CEST'

@chlebowa
Copy link
Contributor

chlebowa commented Oct 18, 2023

See, I don't get those warnings 🤔

> format(as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), usetz = TRUE)
[1] "2023-06-29 00:00:01 CEST" "2023-06-29 00:00:02 CEST" 
> as.POSIXct(format(as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), usetz = TRUE), tz = 'CEST')
[1] "2023-06-29 00:00:01 CEST" "2023-06-29 00:00:02 CEST"
>

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

I just pushed @averissimo proposition, but the restore restores the timestamp into the current timezone of the session, which is not ideal. I will need to revert it and work on short-codes of timezones

x <- teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "UTC"), 
+     fixed = TRUE
+ )
> y <- teal_slices(x)
> y
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 00:00:01 +0000", "2023-06-29 00:00:02 +0000"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> slices_store(y, file = 'store.json')
> z <- slices_restore('store.json')
> z
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-29 02:00:01 +0200", "2023-06-29 02:00:02 +0200"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> class(shiny::isolate(z[[1]]$selected))
[1] "POSIXct" "POSIXt"

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

I wonder if lubridate package could handle that..

@averissimo
Copy link
Contributor

👍 lubridate.. There's a reason why the meme about never working with timezones, it's full of problems.

Is it a big problem to restore the timezone into the local one?

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023 via email

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

Ok guys, let's end this painful process. I know you are all sick and tired of timestamps already!

Based on all edge-cases that we've noticed during this discussion, I assume there is no gold solution in here. I just made final changes and the one that I propose, at least in my opinion, is the least evil.

Even though a timestamp can be passed in any timezone, we do convert it to UTC for print/format but we append UTC to print/format so that this change is visible. Under the hood the timezone is a number so filtering capabilities remain the same, no matter in which timezone we print the number.

Having UTC in print/format (hence store which uses format) allows us to restore POSIX*t in UTC as well, so they are printed in UTC again.

> x <- teal_slice(
+     dataname = "ADSL", 
+     varname = "COUNTRY", 
+     selected = as.POSIXct(c("2023-06-29 00:00:01", "2023-06-29 00:00:02"), tz = "CET"), 
+     fixed = TRUE
+ )
> y <- teal_slices(x)
> y
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-28 22:00:01 UTC", "2023-06-28 22:00:02 UTC"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> slices_store(y, file = 'store.json')
> z <- slices_restore('store.json')
> z
{
  "slices": [
    {
      "dataname" : "ADSL",
      "varname"  : "COUNTRY",
      "id"       : "ADSL COUNTRY",
      "selected" : ["2023-06-28 22:00:01 UTC", "2023-06-28 22:00:02 UTC"],
      "fixed"    : true,
      "anchored" : false,
      "multiple" : true
    }
  ],
  "attributes": {
    "allow_add"  : true
  }
} 
> class(shiny::isolate(z[[1]]$selected))
[1] "POSIXct" "POSIXt" 

@chlebowa
Copy link
Contributor

chlebowa commented Oct 18, 2023

Seems so be working just fine and I see that checks have passed, but some test fail for me locally 🤔

>   time_stamps <- Sys.time() + c(-10 * 60 * 60 * 24, -30, 0)
> 
>   # ISO8601 does not keep milliseconds
>   time_stamps <- as.POSIXct(
+     ceiling(as.double(time_stamps)),
+     tz = "UTC"
+   )
Error in as.POSIXct.numeric(ceiling(as.double(time_stamps)), tz = "UTC") : 
  'origin' must be supplied

@averissimo
Copy link
Contributor

I guess the tests should use 1970-01-01

r> ?as.POSIXct.numeric
...
Optional since R 4.3.0, where the equivalent of "1970-01-01" is used.

@chlebowa
Copy link
Contributor

Optional since R 4.3.0, where the equivalent of "1970-01-01" is used.

That explains it 😒

@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

appended tests. thanks for local check. I think we should extend GITHUB R CMD CHECK with multiple R versions (and preferably multiple platforms)

@m7pr m7pr merged commit d0d9143 into main Oct 18, 2023
24 checks passed
@m7pr m7pr deleted the 373_restore_classes@main branch October 18, 2023 19:14
@m7pr
Copy link
Contributor Author

m7pr commented Oct 18, 2023

Raised a request to start running GHA Checks with multiple R versions https://github.com/insightsengineering/idr-tasks/issues/682

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

Successfully merging this pull request may close these issues.

slices_restore do not restore POSIXT and DATES classes
4 participants