2

I used sink() to write the output of my script to a .txt file but for whatever reason the prompt got written to the txt file too. I asked a question a few days ago to fix that but no answer so here I am approaching it from a different angle. Here is the txt file that got generated by my script:

> geno(hdr) 
DataFrame with 12 rows and 3 columns
            Number        Type                         Description
       <character> <character>                         <character>
GT               1      String                            Genotype
GQ               1     Integer                    Genotype Quality
DP               1     Integer                          Read Depth
HDP              2     Integer                Haplotype Read Depth
HQ               2     Integer                   Haplotype Quality
...            ...         ...                                 ...
mRNA             .      String                     Overlaping mRNA
rmsk             .      String                  Overlaping Repeats
segDup           .      String Overlaping segmentation duplication
rCov             1       Float                   relative Coverage
cPd              1      String                called Ploidy(level)

> sink()

Since the lines I want gone are the ones that start with >, I was thinking if there was a way to open the txt file and remove those particular lines. In this case, the lines > geno(hdr) and > sink() would be removed. I am not good at regex at all in R so I don't know how this would work. Any help appreciated. Thanks.

  • ?maybe `capture.output()` would work instead? – cory Jul 05 '16 at 18:15
  • I tried capture.output() as well. No luck. I really don't know what the issue is that's why I decided to go this route instead of banging my head trying to figure it out – move_slow_break_things Jul 05 '16 at 18:50
  • As commented in your earlier [question](http://stackoverflow.com/questions/38132675/redirect-stdout-to-a-txt-file-in-r-without-the-prompt), the prompts do not show when using `sink()` for me as well. What is your CPU environment: R version/OS/R Studio or RScript or RCmd run of code? – Parfait Jul 05 '16 at 19:02
  • I'm using Rstudio 0.98.1103 on CentOs 6.8. R version 3.3.0 – move_slow_break_things Jul 05 '16 at 20:55

1 Answers1

2

You can try this. Read the file line by line and check if the line starts with >, if not, append it to a new file:

con <- file('test.txt', open = 'r')
while(TRUE) {
    line <- readLines(con, n = 1)
    if(length(line) == 0) break
    else if(!startsWith(line, ">")){
        write(line, file = "newTest.txt", append = TRUE)
    } 
}
Psidom
  • 209,562
  • 33
  • 339
  • 356