[R] Define return values of a function

Soeren.Vogel at eawag.ch Soeren.Vogel at eawag.ch
Sun Nov 22 12:26:33 CET 2009


I have created a function to do something:

i <- factor(sample(c("A", "B", "C", NA), 793, rep=T, prob=c(8, 7, 5,  
1)))
k <- factor(sample(c("X", "Y", "Z", NA), 793, rep=T, prob=c(12, 7, 9,  
1)))
mytable <- function(x){
   xtb <- x
   btx <- x
   # do more with x, not relevant here
   cat("The table has been created, see here:\n")
   print(xtb)
   list(table=xtb, elbat=btx)
}
tbl <- table(i, k)
mytable(tbl) # (1)
z <- mytable(tbl) # (2)
str(z) # (3)

(1) Wanted: outputs the string and the table properly. *Unwanted*:  
outputs the list elements.

(2) and (3) Wanted: outputs the string properly. Wanted: assigns the  
list properly.

How can I get rid of the *unwanted* part? That is, how do I define  
what the functions prints and -- on the other hand -- what it returns  
without printing?

Thanks

Sören

-- 
Sören Vogel, Dipl.-Psych. (Univ.), PhD-Student, Eawag, Dept. SIAM
http://www.eawag.ch, http://sozmod.eawag.ch




More information about the R-help mailing list