[R] How to stop function printing unwanted output?

David Winsemius dwinsemius at comcast.net
Thu Aug 6 18:39:46 CEST 2009


On Aug 6, 2009, at 12:24 PM, Erik Iverson wrote:

>>> In my example, I don't want LSD.test to print any output, I just  
>>> want
>>> to use some of it later... How to stop it printing anything?
>
>> ?invisible
>
>
> I'm not sure that works in this situation:
>
> test <- function() {
>  print("output")
> }
>
> test()
> invisible(test())
>
> Both of the above still print out "output", which I think is the  
> original problem. The invisible function will not print the value of  
> a function, but if there is an explicit "print" call within the  
> function, that will of course happen, since the value hasn't been  
> returned at that point.  However, we don't know the original problem  
> since LSD.test is not defined, and therefore the example is not  
> reproducible.  I assume within the function, it is printing out some  
> values using "print" or "cat"?  We just have to guess though.
>
> Maybe the answer is in ?sink ??
>

Quite right. A search suggests that LSD.test is from package agricolae  
and it does have several points at which cat() and print() would be  
subverting my intent. So the answer would be to sink it "somewhere  
that the sun does not shine" and then "unsink" or would that be  
"resurface"?
With the example on the LSD.test help page:

 > sink(file="undesired.txt")
 > comparison <- invisible( LSD.test(yield,virus,df,MSerror,  
p.adj="bonferroni", group=FALSE,
+ main="Yield of sweetpotato\ndealt with different virus") )
 > sink()
 > comparison
   trt    means M N  std.err
1  cc 24.40000   3 2.084067
2  fc 12.86667   3 1.246774
3  ff 36.33333   3 4.233727
4  oo 36.90000   3 2.482606

> Erik

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list