After appending a new class to a data frame, I can no longer run dplyr functions on the data frame. Two examples here. Initially I can apply dplyr::mutate and dplyr::filter with no problems.
## Create df
df <- data.frame("value" = 1:3)
class(df)
[1] "data.frame"
## Apply mutate or filter
dplyr::mutate(df, value_new = value^2)
dplyr::filter(df, value < 3)
However after adding a class:
## Add a class
class(df) <- append(class(df), "myclass")
class(df)
[1] "data.frame" "myclass"
## Apply mutate or filter
dplyr::mutate(df, value_new = value^2)
dplyr::filter(df, value < 2)
Returns the following errors:
Error in `vec_data()`:
! `x` must be a vector, not a <data.frame/myclass> object.
Run `rlang::last_trace()` to see where the error occurred.
Error in `vec_slice()`:
! `x` must be a vector, not a <data.frame/myclass> object.
Run `rlang::last_trace()` to see where the error occurred.
As I understand, appending a new class to an object whcih already has a class, should not impact how the object is treated when other S3 methods are applied to it, because there is a hierarchical structure (inheritence: https://adv-r.hadley.nz/s3.html#s3-inheritance). However, I a getting a little out of my depth.
I would like to add a new class because I am developing an R package, and I want to assign some new classes to some objects as part of this. I think explaining/understanding the problem above should be sufficient to be able to do this, but I can provide more details about the new S3 generic I am attempting to create if neccesary.
Many thanks
4