You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const R_HELPER_DATETIME = `
EXCEL_BASE <- 25569 # Jan 1, 1970 in Excel date representation
SECONDS_PER_DAY <- 86400 # 24*60*60
convert_to_excel_date <- function(x) {
if (inherits(x, "POSIXlt") || inherits(x, "POSIXct")) {
# Use base_as.POSIXct to prevent recursion
timestamp <- as.numeric(base_as.POSIXct(x))
EXCEL_BASE + timestamp / SECONDS_PER_DAY
}
else if (inherits(x, "Date")) {
as.numeric(x) + EXCEL_BASE
} else if (inherits(x, "difftime")) {
as.numeric(x, units="secs") / SECONDS_PER_DAY
} else if (inherits(x, "times")) { # For time objects from chron package
as.numeric(x) # chron times are already stored as fraction of day
} else x
}
# Save original functions
base_ISOdate <- ISOdate
base_ISOdatetime <- ISOdatetime
base_as.Date <- as.Date
base_as.POSIXct <- as.POSIXct
base_as.POSIXlt <- as.POSIXlt
base_difftime <- difftime
base_times <- chron::times
# Override datetime functions
ISOdate <- function(...) {
convert_to_excel_date(base_ISOdate(...))
}
ISOdatetime <- function(...) {
convert_to_excel_date(base_ISOdatetime(...))
}
as.Date <- function(...) {
convert_to_excel_date(base_as.Date(...))
}
as.POSIXct <- function(...) {
convert_to_excel_date(base_as.POSIXct(...))
}
as.POSIXlt <- function(...) {
convert_to_excel_date(base_as.POSIXlt(...))
}
# For time differences (timedelta equivalent)
difftime <- function(...) {
convert_to_excel_date(base_difftime(...))
}
# For times - assuming chron is already loaded
times <- function(...) {
convert_to_excel_date(base_times(...))
}
`
this gives me answers relevant to Excel when using R, but, there are following 2 requirements:
composition i.e. difftime(as.POSIXct("2020-05-17 12:00:00"), as.POSIXct("2020-05-17 00:00:00"), units="hours") should work. But due to global overriding, I have to do something like this
list of such objects should also use this converter so replicate(3, as.POSIXct("2020-05-17 00:00:00"), simplify=FALSE) should give me list of 3 values, which are 43967.77083333333 each i.e. it is applied recursively...
all of this needs to happen implicitly in some sense.. i.e. without user knowing about it..
Is there a mechanism to supply a custom serialiser etc. such that the above method is applied implicitly? A hook of some sorts basically.
Unfortunately, this is not yet available in webR. WebR handles its own serialisation and deserialisation for communicating between the JavaScript worker thread and the main thread, and right now there is no hook for custom serialisation routines.
However, we do plan to eventually rework the replacer and reviver used for serialisation over the communication channel. This will provide a natural place for a hook similar to default_converter to be added. I would say it's likely that when we eventually introduce this, the API will be similar to Pyodide's.
Consider the following snippet:
this gives me answers relevant to Excel when using R, but, there are following 2 requirements:
difftime(as.POSIXct("2020-05-17 12:00:00"), as.POSIXct("2020-05-17 00:00:00"), units="hours")
should work. But due to global overriding, I have to do something like thisreplicate(3, as.POSIXct("2020-05-17 00:00:00"), simplify=FALSE)
should give me list of 3 values, which are43967.77083333333
each i.e. it is applied recursively...all of this needs to happen implicitly in some sense.. i.e. without user knowing about it..
Is there a mechanism to supply a custom serialiser etc. such that the above method is applied implicitly? A hook of some sorts basically.
AFAIK, pyodide offers one here: https://pyodide.org/en/latest/usage/api/python-api/ffi.html#pyodide.ffi.JsProxy.to_py
see the
default_converter
argument..I tried playing around with
REnvironment
and thedepth
parameter oftoJs()
but I wasn't able to make it work..The text was updated successfully, but these errors were encountered: