Closed jeffkimbrel closed 11 months ago
Have you tried using a custom constructor
?
Thanks, that was obvious but not so obvious. All of the example custom constructors I could find only had a new_object
call, and I didn't realize more code could be added. I also didn't realize the arguments to the constructor are the props, and that multiple could be passed. So, this code works:
cars_object2 <- S7::new_class(
"cars_object2",
properties = list(
data = S7::class_data.frame,
horsepower_column_name = S7::class_character
),
constructor = function(data, horsepower_column_name) {
data = dplyr::rename(data,
horsepower = horsepower_column_name)
S7::new_object(S7::S7_object(),
data = data,
horsepower_column_name = horsepower_column_name)
}
)
Thanks again for the help! Also, would you consider opening up the "discussions" portion of the repo? This wasn't really an "issue" and might have fit better under a discussion.
I have an S7 class where a user will upload a data frame into a slot called
@data
, and then there are other slots where a user can define the column names where other expected data will be found. What I want to do is take their data frame and do an actualdplyr::rename
to change the column name in@data
from the name they provide to a standardized column name, modifying the@data
between when it is given to the S7 instantiation and actually saved to the new class. Is it possible to do such modifications?For example, if a user provides
mtcars
with thehp
column and I want to change it tohorsepower
during instantiation...I can modify the S7 object afterward with:
but is there any way to put it in the actual
new_class
call? Maybe part of a custom constructor or something?Alternatively, is it better to make a wrapper around the
new_class
call to handle this? This does work, but is it recommended to use such wrappers around the creation of S7 objects?Thanks for any help.