[R] Help with Sink Function

Matt Shotwell shotwelm at musc.edu
Fri Jul 16 20:55:36 CEST 2010


Your code between calls to sink() does not generate any output. Hence,
nothing will be diverted to the file. To illustrate this point,
consider 

for(i in 1:10) i

This produces no output. However, 

for(i in 1:10) print(i)

produces output as expected.

-Matt

On Fri, 2010-07-16 at 13:34 -0400, Addi Wei wrote:
> Sorry about that. Still new to this...  The code below should be
> reproducible.    All R2 should just be 1, and I should write 1 to
> R2outputKKNN.txt 10 times....nothing is happening.  Appreciate the efforts
> to help! 
> 
> for (i in 1:10)
> {
>       	adata = 1:5
>  	bdata = 6:10
> 	lm <- lm(adata~bdata)
> 	slm <- summary(lm)
>         str(slm)
> 
>        if (i == 1)     {               
> 		previousR2 <-slm$r.squared
>                sink(file="R2outputKKNN.txt", append=TRUE)
>                previousR2                      
>                sink()  }       else if(i!=1)
>        {
>                currentR2 <- slm$r.squared
>                if (previousR2 > currentR2)
>                {
>                        currentR2 <- previousR2        
>                }      
>                if (previousR2 < currentR2)             {
>                        sink(file="R2outputKKNN.txt", append=TRUE)
>                        currentR2
>                        sink()  
>                }
>        }
> }
-- 
Matthew S. Shotwell
Graduate Student
Division of Biostatistics and Epidemiology
Medical University of South Carolina
http://biostatmatt.com



More information about the R-help mailing list