Daniel Caro
2015-Jul-13 21:06 UTC
[R] Function returning multiple objects but printing only one
Hello,
Sorry if this has already been addressed before but I could not find any
helpful references.
I would like to create a function that outputs a single element of a list
but stores all elements, similar to 'lm' and many other functions.
There
are several answers on how to return multiple objects with lists, for
example:
http://r.789695.n4.nabble.com/How-to-return-multiple-values-in-a-function-td858528.html
http://stackoverflow.com/questions/8936099/returning-multiple-objects-in-an-r-function
But the examples show how to print multiple outputs, such as
functionReturningTwoValues <- function() {return(list(first=1, second=2))}
functionReturningTwoValues()
And I only want the function to print a single element from the list but
still store the other elements such that they can be retrieved with
functionReturningTwoValues$first, for example. My function produces
bootstrap coefficients so clearly I don't want to print the bootstrap
output but I do want users to be able to access it.
Many thanks,
Daniel
[[alternative HTML version deleted]]
Duncan Murdoch
2015-Jul-14 11:17 UTC
[R] Function returning multiple objects but printing only one
On 13/07/2015 5:06 PM, Daniel Caro wrote:> Hello, > > Sorry if this has already been addressed before but I could not find any > helpful references. > > I would like to create a function that outputs a single element of a list > but stores all elements, similar to 'lm' and many other functions. There > are several answers on how to return multiple objects with lists, for > example: > > http://r.789695.n4.nabble.com/How-to-return-multiple-values-in-a-function-td858528.html > > http://stackoverflow.com/questions/8936099/returning-multiple-objects-in-an-r-function > > But the examples show how to print multiple outputs, such as > > functionReturningTwoValues <- function() {return(list(first=1, second=2))} > functionReturningTwoValues() > > And I only want the function to print a single element from the list but > still store the other elements such that they can be retrieved with > functionReturningTwoValues$first, for example. My function produces > bootstrap coefficients so clearly I don't want to print the bootstrap > output but I do want users to be able to access it.You need to give your object a class, and define a print method for that class. It's pretty simple: functionReturningTwoValues <- function() {return(structure(list(first=1, second=2), class="MyClass"))} print.MyClass <- function(x, ...) { print(x$first, ...) } This is using "S3 classes". There are other systems (S4, etc.) that let you do this, but none are simpler. Duncan Murdoch
Adams, Jean
2015-Jul-14 12:54 UTC
[R] Function returning multiple objects but printing only one
Daniel,
I'm not sure if this is what you're after, but you could include a
print()
call in your function. For example:
myfun <- function(x) {
m1 <- min(x)
m2 <- mean(x)
m3 <- max(x)
out <- list(m1, m2, m3)
print(out[[2]])
return(out)
}
result <- myfun(1:10)
?Jean
?
On Mon, Jul 13, 2015 at 4:06 PM, Daniel Caro <dcarov at gmail.com> wrote:
> Hello,
>
> Sorry if this has already been addressed before but I could not find any
> helpful references.
>
> I would like to create a function that outputs a single element of a list
> but stores all elements, similar to 'lm' and many other functions.
There
> are several answers on how to return multiple objects with lists, for
> example:
>
>
>
http://r.789695.n4.nabble.com/How-to-return-multiple-values-in-a-function-td858528.html
>
>
>
http://stackoverflow.com/questions/8936099/returning-multiple-objects-in-an-r-function
>
> But the examples show how to print multiple outputs, such as
>
> functionReturningTwoValues <- function() {return(list(first=1,
second=2))}
> functionReturningTwoValues()
>
> And I only want the function to print a single element from the list but
> still store the other elements such that they can be retrieved with
> functionReturningTwoValues$first, for example. My function produces
> bootstrap coefficients so clearly I don't want to print the bootstrap
> output but I do want users to be able to access it.
>
> Many thanks,
> Daniel
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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]]