[R] passing a modified argument to an S3 method

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sat Apr 20 17:51:23 CEST 2024


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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list