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
Thanks for the workaround! I was able to apply it to my real use case.
Just for reference to anyone else reading this, here's what you'd do if you still want to rely on referencing an external object (the penguins data frame) to get the property names:
library(S7)
library(rlang)
library(purrr)
library(palmerpenguins)
my_penguin<- as.list(penguins[1,])
penguin<- new_class(
"penguin",
properties=list(
species=class_factor,
island=class_factor,
bill_length_mm=class_double,
bill_depth_mm=class_double,
flipper_length_mm=class_integer,
body_mass_g=class_integer,
sex=class_factor,
year=class_integer
),
constructor=function(x) {
props<- set_names(colnames(penguins)) # Get prop names from `penguins` tblprop_vals<- map(props, function(prop_name) pluck(x, prop_name))
# hack to enable using `new_object()` with NSE.constructor<-function() {}
body(constructor) <- inject(quote(new_object(S7_object(), !!!prop_vals)))
attributes(constructor) <- attributes(sys.function())
constructor()
}
)
penguin(my_penguin)
#> <penguin>#> @ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1#> @ island : Factor w/ 3 levels "Biscoe","Dream",..: 3#> @ bill_length_mm : num 39.1#> @ bill_depth_mm : num 18.7#> @ flipper_length_mm: int 181#> @ body_mass_g : int 3750#> @ sex : Factor w/ 2 levels "female","male": 2#> @ year : int 2007
Thanks for the workaround! I was able to apply it to my real use case.
Just for reference to anyone else reading this, here's what you'd do if you still want to rely on referencing an external object (the
penguins
data frame) to get the property names:Created on 2024-11-19 with reprex v2.1.1
Originally posted by @mccarthy-m-g in RConsortium/S7#497 (comment)
The text was updated successfully, but these errors were encountered: