0

I have two questions regarding to exporting results to txt file.

First is, getting empty text file when using "sink" function with loop. result is lists - summary(aov.res) code below did make six text files, but nothing inside(empty text file).

i = 1
repeat{
tableA<-subset(coral, Location=="D7")
tableB<-subset(coral, Location==unique(coral$Location)[i])
tab<-rbind(tableA,tableB)
aov.res<-aov(cbind(AJ,A,DG,SG,EA)~Location*BvA,data = tab)
sink(paste("BA X CI", i,".txt"))
summary(aov.res)
sink()
if(i >= 6) break
i = i+1
}

so I had to do make code for each i. such like,

i=1
tableA<-subset(coral, BvA == "Before" & Location=="D7")
tableB<-subset(coral, BvA == "Before" & Location==unique(coral$Location)[i])
tab<-rbind(tableA,tableB)
aov.res<-aov(cbind(AJ,A,DG,SG,EA)~Location*Season,data = tab)
sink(paste("Short(bef) X CI","-Control vs D",i,".txt"))
summary(aov.res)
sink()

i=2
##same code as above, (tableA <-subset~~~~~~sink())
i=3
##same code as above,...

until i=6

Second is, how to make file name using variables. Another approach I tried to solve the matter was using "capture.output" function. However, bit stuck with making file name using variables(i), code is below.

    i = 1
    repeat{
    tableA<-subset(coral, Location=="D7")
    tableB<-subset(coral, Location==unique(coral$Location)[i])
    tab<-rbind(tableA,tableB)
    aov.res<-aov(cbind(AJ,A,DG,SG,EA)~Location*BvA,data = tab)
    capture.output(summary(aov.res), file ="BA X CI", i, ".txt") 
    if(i >= 6) break
    i = i+1
    }

code (obviously...) produced "BA X CI" file though I wanted six files named "BA X CI n.txt" (n 1~6).

Does anyone give some tips please?? Thank you so much!!

KELPman
  • 13
  • 3
  • 1
    You probably need to replace `summary(aov.res)` with `print(summary(aov.res))`. – Mikael Jagan Nov 20 '21 at 17:02
  • 1
    As for your second question, `file = paste0(prefix, " ", i, ".txt")` should return the strings you want, but you should [really](https://superuser.com/questions/29111/what-technical-reasons-exist-for-not-using-space-characters-in-file-names) avoid spaces in file names... – Mikael Jagan Nov 20 '21 at 17:15

0 Answers0