Skip to content

Best way to export values from a function?

13 messages · Jason Rupert, Daniel Nordlund, jim holtman +6 more

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

  
    
#
On Wed, Jul 8, 2009 at 5:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
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:
Yeah, R is difficult to search for - I've had partial success with
rseek.org, though.
You can always do:
But then you'd have to call it like so:
[,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:
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
#
On Wed, Jul 8, 2009 at 5:55 PM, Mark Knecht<markknecht at gmail.com> wrote:
<SNIP>

This version runs a bit better than my last and I find it a bit more
readable, but there's a warning for whatever the first computation is
inside the function that I'd like to understand.

- Mark

multipleoutput <- function(x) {
	answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
	answer$Squ=x^2
	answer$Cub<-x^3
	answer$Exp<-exp(x)
	answer$Fac<-factorial(x)
	return(answer)
}

X = data.frame("Squared"=0,"Cubed"=0,"Exp"=0,"Fac"=0)
X
mode(X)
names(X)

X[1,] <- multipleoutput(2)

X
class(X)

<PRODUCES>
+ answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
+ answer$Squ=x^2
+ answer$Cub<-x^3
+ answer$Exp<-exp(x)
+ answer$Fac<-factorial(x)
+ return(answer)
+ }
Squared Cubed Exp Fac
1       0     0   0   0
[1] "list"
[1] "Squared" "Cubed"   "Exp"     "Fac"
Warning message:
In answer$Squ = x^2 : Coercing LHS to a list
Squared Cubed      Exp Fac
1       4     8 7.389056   2
[1] "data.frame"
#
Mark and Jason,
At 7:23 PM -0700 7/8/09, Mark Knecht wrote:
?list

e.g.,

multipleoutput <- function(x) {
	answer = list()
	answer$Squ=x^2
	answer$Cub<-x^3
	answer$Exp<-exp(x)
	answer$Fac<-factorial(x)
	return(answer)
}


Bill
#
Hi,
On Jul 8, 2009, at 8:34 PM, Jason Rupert wrote:

            
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
#
Hi

r-help-bounces at r-project.org napsal dne 09.07.2009 02:57:33:
wrote:
this
function,
exp = exp(x))) }
Not so weird. What do you expect from

c(1:5, 10:20, 30:50)

That is basically what your function do. With slight modification you can 
get tabular output without mapply

multipleoutput <- function (x) {
result.s <- x^2
result.c <- x^3
result.e <- exp(x)
cbind(square=result.s, cube=result.c, exp=result.e)
}

If the output could be mixed type }numeric, character, ...) use data.frame 
instead of cbind

Regards
Petr
http://www.R-project.org/posting-guide.html
#
Hi

Godmar Back <godmar at gmail.com> napsal dne 09.07.2009 14:09:42:
wrote:
<snip>
sequence
arrays.
[1] FALSE
[1] TRUE
Why. Not much is said about arrays in man page for c. The operator 
concatenates its arguments. If they are vectors the output is again a 
vector. If they are lists the output is list.

try
c(1:5,10:20)
c(1:5, list(10:20)
c(1:5, data.frame(10:20)

and regarding arrays, matrices and vectors - AFAIK arrays and matrices are 
just vectors with dim attribute, which is stripped by c 
 
c is sometimes used for its side effect of removing attributes except 
names, for example to turn an array into a vector. as.vector is a more 
intuitive way to do this, but also drops names. Note too that methods 
other than the default are not required to do this (and they will almost 
certainly preserve a class attribute).
can
the
languages
No. AFAIK one dot does not have any special meaning unless you use it in 
the beginning of name. In that case the variable is not listed by ls() 
function. See ?ls and argument all.names

Regards
Petr