[R] Retrieving objects from functions...

Gabor Grothendieck ggrothendieck at myway.com
Thu Sep 30 10:08:07 CEST 2004

Jonathan Harris <jharris <at> hss.caltech.edu> writes:

: I've written a program that involves a loop that creates a matrix. I'd 
: like to be able to manipulate that matrix on my R desktop, but after I 
: run the function, that matrix does not appear when I type ls(). How can 
: I make that matrix become an object that I can manipulate?

Just return the matrix as the value of your function.  If you are already
returning something else as the value of the function return a list with
that value and the matrix as the two components.

Check out the builtin eigen function is an example of this as it returns 
both eigenvalues and eigenvectors.  

The aforementioned is probably the best strategy but if you must then
you could alternately assign the matrix into the parent or global environment
from within your function using assign:

assign("mat", mat, parent.frame())  
assign("mat", mat, .GlobalEnv)

More information about the R-help mailing list