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

mechanism to hook into webR's serialisation pipeline? #500

Open
madhur-tandon opened this issue Nov 17, 2024 · 1 comment
Open

mechanism to hook into webR's serialisation pipeline? #500

madhur-tandon opened this issue Nov 17, 2024 · 1 comment
Labels
feature New feature or request

Comments

@madhur-tandon
Copy link

Consider the following snippet:

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
difftime(base_as.POSIXct("2020-05-17 12:00:00"), base_as.POSIXct("2020-05-17 00:00:00"), units="hours")
  • 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.

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 the depth parameter of toJs() but I wasn't able to make it work..

@georgestagg georgestagg added the feature New feature or request label Dec 16, 2024
@georgestagg
Copy link
Member

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.

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

No branches or pull requests

2 participants