[R] passing a modified argument to an S3 method

CRAN.r cr@n@r @end|ng |rom proton@me
Sat Apr 20 18:02:53 CEST 2024


I've got a complicated default value for an argument, basically a "control" argument that's a list with a lot of defaults, and I want to figure out its value before dispatching to a method so I don't need to have the same code repeated at the beginning of every method. None of the default values should be method-dependent, so I don't need or want each method to figure out the value separately. I hope that helps!

Jay


On Saturday, April 20th, 2024 at 10:51 AM, Bert Gunter <bgunter.4567 using gmail.com> wrote:

> I do not understand what your goal is here (more context may be helpful, perhaps to others rather than me). So I doubt this is what you want, but here is a guess -- no need to respond if it is unhelpful:
> 
> ## test.default returns NULL if object "y" not found in **calling environment and enclosures**;
> ## otherwise y.
> 
> test <- function(x){
> UseMethod("test")
> }
> test.default <- function(x){
> tryCatch(y, error = function(e)NULL)
> }
> 
> ## y not found
> test(x=3)
> NULL
> 
> ## y found
> > y <- 'abcd'
> > test(x = 3)
> [1] "abcd"
> 
> Cheers,
> Bert
> 
> 
> 
> 
> On Sat, Apr 20, 2024 at 4:23 AM CRAN.r via R-help <r-help using r-project.org> wrote:
> 
> > Is there a way to pass a modified argument from an S3 generic to a method? Here's a non-working example that I want to return "abcd".
> > 
> > test <- function(x, y = NULL){
> > y <- "abcd"
> > UseMethod("test")
> > }
> > test.default <- function(x, y = NULL) y
> > test(x = 3)
> > 
> > Is that possible? I've looked around a lot, but can't find any examples or discussion.
> > 
> > Jay
> > 
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list