Is there a way to modify all of an object's methods?

Background is that I'm working on a package which creates an un-writable dataframe-like object which let's call fancy_df. The way I'm accomplishing this is writing methods for write.csv and other output common output functions which will ask the user to confirm that they really want to save the file.

My issue is that I want to have the object inherit from the data.frame class and so have access to all of those methods, but have all those methods return fancy_dfs, but I don't want to write all those methods myself. In my mind I'm thinking of a catch all method that would be do the following for all data.frame methods:

f.fancy_df <- function(...) { as_fancy_df(f(...))}

Does anyone know how to do this? Or alternatively if there's a name for what I'm trying to do?

This is all for an internal package which will never see CRAN.

FWIW

methods(class="data.frame")

returns alot (152 in my uncleansed R session) of functions and not all of them return data.frames and it's not necessarily super-easy to determine return type.

Right, although I could probably check to only do the class coercion if they did return a dataframe. Maybe it is better to just be explicit and define all of the common methods.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.