Paul Laub
2007-Jun-30 10:44 UTC
[R] Determining whether a function's return value is assigned
Dear all, Does R offer a means by which a function can determine whether its return value is assigned? I am using R 2.4.1 for Windows. Suppose what I am looking for is called "return.value.assigned". Then one might use it like this myfunction <- function () { # Create bigobject here if (return.value.assigned()) { bigobject } else { summary(bigobject) } } and x <- myfunction() # bigobject is assigned myfunction() # summary of bigobject is printed Octave and MATLAB have the nargout function that does what I want, and Perl has the wantarray function detecting the context in which a function is called. Perhaps match.call() can be made to do what I want, but, if so, I don't see it in reading the documentation. Sincerely, Paul Laub
Uwe Ligges
2007-Jun-30 13:58 UTC
[R] Determining whether a function's return value is assigned
Paul Laub wrote:> Dear all, > > Does R offer a means by which a function can determine > whether its return value is assigned? I am using R > 2.4.1 for Windows.Short answer: No. Long answer: You want to have a class for the object called "bigobject" below and a print/show method that provides the summary for objects of that class. Uwe Ligges> > Suppose what I am looking for is called > "return.value.assigned". Then one might use it like > this > > myfunction <- function () { > # Create bigobject here > > if (return.value.assigned()) { > bigobject > } else { > summary(bigobject) > } > } > > and > > x <- myfunction() # bigobject is assigned > > myfunction() # summary of bigobject is printed > > Octave and MATLAB have the nargout function that does > what I want, and Perl has the wantarray function > detecting the context in which a function is called. > Perhaps match.call() can be made to do what I want, > but, if so, I don't see it in reading the > documentation. > > Sincerely, > > Paul Laub > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > 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.
Thomas Lumley
2007-Jul-02 14:16 UTC
[R] Determining whether a function's return value is assigned
On Sat, 30 Jun 2007, Paul Laub wrote:> Dear all, > > Does R offer a means by which a function can determine > whether its return value is assigned? I am using R > 2.4.1 for Windows.No> Suppose what I am looking for is called > "return.value.assigned". Then one might use it like > this > > myfunction <- function () { > # Create bigobject here > > if (return.value.assigned()) { > bigobject > } else { > summary(bigobject) > } > }That's what the print() function is for. Write a print() method. -thomas