[R] How to stop function printing unwanted output?

ml-r-help at epigenomics.com ml-r-help at epigenomics.com
Thu Aug 6 19:39:31 CEST 2009


?capture.output

so with the given example this line should do what you wanted while avoiding the output file

dummy <- capture.output(comparison <- LSD.test(yield,virus,df,MSerror, p.adj="bonferroni",
group=FALSE, main="Yield of sweetpotato\ndealt with different virus"))

Matthias

David Winsemius wrote:
> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


-- 
Matthias Burger                     Project Manager/ Biostatistician
Epigenomics AG    Kleine Praesidentenstr. 1    10178 Berlin, Germany
phone:+49-30-24345-0                            fax:+49-30-24345-555
http://www.epigenomics.com           matthias.burger at epigenomics.com
--
Epigenomics AG Berlin           Amtsgericht Charlottenburg HRB 75861
Vorstand:                           Geert Nygaard (CEO/Vorsitzender)
                                            Oliver Schacht PhD (CFO)
Aufsichtsrat:   Prof. Dr. Dr. hc. Rolf Krebs (Chairman/Vorsitzender)




More information about the R-help mailing list