RConsortium / S7

S7: a new OO system for R
https://rconsortium.github.io/S7
Other
386 stars 32 forks source link

Abbreviating `str()` #378

Closed jl5000 closed 9 months ago

jl5000 commented 9 months ago

I have some classes which define many properties, only some of which may have a value. The str() function displays all properties, even those of zero-length. Is there a way to only display properties with only a non-zero length?

library(S7)

test_class <- new_class("test_class",
                        properties = list(
                          x = class_list,
                          y = class_character,
                          z = class_numeric
                        ))

obj <- test_class(z = 1)

str(obj)
#> <test_class>
#>  @ x: list()
#>  @ y: chr(0) 
#>  @ z: num 1

# Desired:
#> <test_class>
#>  @ z: num 1

Created on 2023-11-19 with reprex v2.0.2

hadley commented 9 months ago

Just because the property is empty doesn't mean it doesn't exist, so I think this is the right default display. You can always write your own custom str() method.