[R] lapply without return values?

Rainer M Krug r.m.krug at gmail.com
Fri Jan 25 10:06:59 CET 2008


Berwin A Turlach wrote:
> G'day Rainer,
> 
> On Fri, 25 Jan 2008 10:34:32 +0200
> Rainer M Krug <r.m.krug at gmail.com> wrote:
> 
> [...] 
> 
>>  > p <- data.frame(runif(10), runif(10), runif(10))
>>  > lapply( p, function(ps) {x11(); plot(ps)} )
>>
>> which results in three graphs and a printout:
> [...]
>> How can I avoid this printout without using
>>
>> tmp <- lapply( p, function(ps) {x11(); plot(ps)} )?
> 
> ?invisible
> 
> like in invisible(lapply( p, function(ps) {x11(); plot(ps)} ))

thanks - that's an option.
But the idea is to prevent the creation of an object - and even if I 
don't assign it to a variable, isn't that object already created in the 
function lapply()? I would like to save on time and memory (I know - not 
necessary, but I would prefer to.)

But I will remember the suggestion about the invisible()


> 
> Note, your solution seems to involve less keystroke but has the
> disadvantage of creating an object in your workspace.  

true - I should do
 > rm(tmp) afterwards

> 
> Of course, you could always do something like:
> 
>> ilapply <- function(...) invisible(lapply(...))
> 
> ## perhaps better:
> ## ilapply <- function(X, FUN, ...) invisible(lapply(X, FUN, ...))
> 
>> ilapply(p, function(ps) {x11(); plot(ps)})
> 
> To save keystrokes in the long run. :)
> 
> HTH.
> 
> Cheers,
> 
> 	Berwin
> 
> =========================== Full address =============================
> Berwin A Turlach                            Tel.: +65 6515 4416 (secr)
> Dept of Statistics and Applied Probability        +65 6515 6650 (self)
> Faculty of Science                          FAX : +65 6872 3919       
> National University of Singapore     
> 6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
> Singapore 117546                    http://www.stat.nus.edu.sg/~statba



More information about the R-help mailing list