Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down. I would like to know the best way to export several doubles from a function, where the doubles are not an array. Here is a contrived function similar to my needs: multipleoutput<-function(x) { squared<-x^2 cubed<-x^3 exponentioal<-exp(x) factorialVal<-factorial(x) } Thanks again for all your help.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Jason Rupert > Sent: Wednesday, July 08, 2009 5:35 PM > To: R-help at r-project.org > Subject: [R] Best way to export values from a function? > > > Maybe there is a great website out there or white paper that > discusses this but again my Google skills (or lack there of) > let me down. > > I would like to know the best way to export several doubles > from a function, where the doubles are not an array. > > Here is a contrived function similar to my needs: > > multipleoutput<-function(x) > { > squared<-x^2 > cubed<-x^3 > exponentioal<-exp(x) > factorialVal<-factorial(x) > > } >I can't vouch for the best way, but here is one way, return a list of the values multipleoutput<-function(x) { squared<-x^2 cubed<-x^3 exponential<-exp(x) factorialVal<-factorial(x) return(list(squared=squared, cubed=cubed, exponential=exponential, factorialVal=factorialVal)) } Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
You can return a list, vector, or any other object: The last value is the return value unless you do an explicit return() On Wed, Jul 8, 2009 at 8:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:> > Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down. > > I would like to know the best way to export several doubles from a function, where the doubles are not an array. > > Here is a contrived function similar to my needs: > > multipleoutput<-function(x) > { > ? ? ? ?squared<-x^2 > ? ? ? ?cubed<-x^3 > ? ? ? ?exponentioal<-exp(x) > ? ? ? ?factorialVal<-factorial(x) > > } > > Thanks again for all your help. > > ______________________________________________ > R-help at r-project.org 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Wed, Jul 8, 2009 at 5:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:> > Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down. > > I would like to know the best way to export several doubles from a function, where the doubles are not an array. > > Here is a contrived function similar to my needs: > > multipleoutput<-function(x) > { > ? ? ? ?squared<-x^2 > ? ? ? ?cubed<-x^3 > ? ? ? ?exponentioal<-exp(x) > ? ? ? ?factorialVal<-factorial(x) > > } > > Thanks again for all your help. > > ______________________________________________ > R-help at r-project.org 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. >I'm a newbie so mine;s probably not the best but it seems to work: multipleoutput<-function(x) { answer<- c(0,0,0,0) MyFuncNames <- c("Squared","Cubed","Exp","Fac") answer$squared<-x^2 answer$cubed<-x^3 answer$exponential<-exp(x) answer$factorial<-factorial(x) return(answer) } X = c(0,0,0,0) X mode(X) names(X) MyNames = c("Squared","Cubed","Exp","Fac") MyNames names(X) = MyNames X <- multipleoutput(2) X class(X) dim(X)
On Wed, Jul 8, 2009 at 8:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:> > Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.Yeah, R is difficult to search for - I've had partial success with rseek.org, though.> > I would like to know the best way to export several doubles from a function, where the doubles are not an array. > > Here is a contrived function similar to my needs: > > multipleoutput<-function(x) > { > ? ? ? ?squared<-x^2 > ? ? ? ?cubed<-x^3 > ? ? ? ?exponentioal<-exp(x) > ? ? ? ?factorialVal<-factorial(x) > > }You can always do:> multipleoutput <- function (x) { return (c(square = x^2, cube = x^3, exp = exp(x))) }But then you'd have to call it like so:> mapply(multipleoutput, c(0,1,2))[,1] [,2] [,3] square 0 1.000000 4.000000 cube 0 1.000000 8.000000 exp 1 2.718282 7.389056 If you call it like so:> multipleoutput(c(0,1,2))square1 square2 square3 cube1 cube2 cube3 exp1 exp2 0.000000 1.000000 4.000000 0.000000 1.000000 8.000000 1.000000 2.718282 exp3 7.389056 then R flattens the result. Weird. - Godmar
Hi, On Jul 8, 2009, at 8:34 PM, Jason Rupert wrote:> > Maybe there is a great website out there or white paper that > discusses this but again my Google skills (or lack there of) let me > down. > > I would like to know the best way to export several doubles from a > function, where the doubles are not an array. > > Here is a contrived function similar to my needs: > > multipleoutput<-function(x) > { > squared<-x^2 > cubed<-x^3 > exponentioal<-exp(x) > factorialVal<-factorial(x) > > }There already have been some suggestions on how to do this the "normal" R way, so let's go ahead and use the "return a list" method (I think it's better than using the `c(squared=x^2, cubed=...)`). Here's an interesting way to receive the assignments. Check out this function: http://code.google.com/p/miscell/source/browse/rvalues/rvalues.r With that ':=' function loaded, you could do this: ===========multipleout <- function(x) { list(squared=x^2, cubed=x^3, exponential=exp(x), factorial=factorial(x)) } c(sq,cu,ex,fa) := multipleout(1:3) show(sq) [1] 1 4 9 show(cu) [1] 1 8 27 show(ex) [1] 2.718282 7.389056 20.085537 show(fa) [1] 1 2 6 ============ [I can't remember how I stumbled onto this code for the ':=' function (I think it was from a thread on the BioC list about package updates)] I'm not saying that you *should* do it this way, but it's kind of cool that you could ... -steve -- Steve Lianoglou Graduate Student: Physiology, Biophysics and Systems Biology Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos