[R] with for objects

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Dec 2 11:39:57 CET 2003


Hadley Wickham wrote:
> Yes, that is a (big) problem.  What do you suggest as an alternative?
> I am experimenting visualising microarray data using trellis.  I end up 
> with a whole lot of calls like xyplot(maA(swirl[,1]) ~ maM(swirl[,1]) | 
> maPrintTip(swirl[,1])) and then if I want to plot the same thing for 
> array 2, I need xyplot(maA(swirl[,2]) ~ maM(swirl[,2]) | 
> maPrintTip(swirl[,2]))...  I don't want to limit myself to a fixed set 
> of plots because I'm still exploring different ways to look at the data, 
> but I would like to save myself some typing.
> 

Could you not build the functions in a loop, using combinations of 
paste, eval and do.call ? I tried this method recently to fit a few 
thousand response curves to my species data using glm() [yes, most were 
completely uninteresting].

There is an excellent example of building function calls in RNews Vol2 
Issue 2 by Bill Venables (Programmer's Niche pages 24--26):

http://cran.r-project.org/doc/Rnews/Rnews_2002-2.pdf

This has the advantage of using the variable names in your plots/calls 
rather than the swirl[,1] format you have used.

Alternatively, grab the number of columns in swirl and then loop over 
your call replacing swirl[,1] with swirl[,i] e.g. [not tried]:

for (i in 1:length(swirl))
{
   xyplot(maA(swirl[,i]) ~ maM(swirl[,i]) | maPrintTip(swirl[,i]))
}

Wrap the whole thing in postscript() or pdf() so all the plots end up in 
a file you can look at later. Not sure whether you need to wrap the 
xyplot() in print() to get lattice to produce the plot in a loop (you do 
in a function for example) as I haven't used lattice much yet, but I'm 
sure someone on the list will correct/advise if needed.

HTH

Gav
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list