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

dump(<S7_object>)? #524

Open
HenrikBengtsson opened this issue Jan 23, 2025 · 1 comment
Open

dump(<S7_object>)? #524

HenrikBengtsson opened this issue Jan 23, 2025 · 1 comment

Comments

@HenrikBengtsson
Copy link
Collaborator

I'm late to the party, and sorry if I missed a discussion on this, but is there a way to dump() an S7 object? Attempting to do so gives:

> x <- S7::S7_object()
> dump("x", file = stdout())
x <-
<object>
Warning message:
In dump("x", file = stdout()) : deparse may be incomplete

which is not valid R code.

Ultimately, I want to create a minimal, valid S7_object object without using the 'S7' package. I want this in order to write a teeny 'codetools'-related unit test, without having to depend on the 'S7' package. Thus far, I know I can save an S7_object object to an RDS file, and obtain such a "creature" without having the 'S7' package installed, e.g.

> z <- readRDS("s7.rds")
> z
<object>
attr(,"class")
[1] "S7_object"

but it would be nice not to rely on a binary representation for my unit tests.

@HenrikBengtsson HenrikBengtsson changed the title dump()? dump(<S7_object>)? Jan 23, 2025
@t-kalinowski
Copy link
Member

The return value from S7_object() is a OBJSXP (formerly known as S4SXP), but with the S4 bit unset. For a time I thought it was only possible to make this object from C.

However, @ltierney did find a way to make it in R at some point:

mkObj <- function(...) {
  ob <- asS3(getClass("S4")@prototype, complete = FALSE) # "hack"
  if (...length())
    attributes(ob) <- list(...)
  ob
}


identical(mkObj(class = "S7_object"), S7::S7_object())
#> [1] TRUE

This doesn't solve the need for better serialization support, but hopefully it unblocks you.

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

No branches or pull requests

2 participants