3

I tried some options from stackoverflow(e.g.1) but this also doens't work so maybe there is a mistake in my code:

 fileConn<-file("outputR.txt")
for (i in 1:length(lines)){
  line = lines[i]
  fields = strsplit(line, "\t")[[1]]
  id = fields[1]
  goIDs = fields[2:length(fields)]
  list = as.list(GOCCANCESTOR[goIDs])
  text = paste(toString(id), ":", toString(goIDs))
  cat(text, file=fileConn, append=TRUE, sep = "\n")

}
close(fileConn)

when I run this code it keeps overwriting the data in the outputR.txt file. Any suggestions to fix this problem?

Community
  • 1
  • 1
  • You can find your answer in [this](http://stackoverflow.com/questions/17710469/why-wont-cat-append-to-a-file-connection?rq=1) post. – dertomtom Oct 09 '16 at 10:12

3 Answers3

2

the problem is that you are using a Fileconnection in combination with cat then the append won't work. There are several option you could use, the most easy one is to this:
first "create" the file, if you want to add a header for example:

header = "some header" 
## if you don't want to use a header then leave the header blank
header =""
cat(text, file="outputR.txt", append=FALSE, sep = "\n")

notice the append = FALSE this is necessary if you want to clear the file if it already exist otherwise you have to use append = TRUE
the you can write text to it using:

text = text = paste(toString(id), ":", toString(goIDs))
cat(text file="outputR.txt", append=TRUE, sep = "\n")
KingBoomie
  • 278
  • 1
  • 12
0

As the help page for cat states:

append: logical. Only used if the argument file is the name of file (and not a connection or "|cmd"). If TRUE output will be appended to file; otherwise, it will overwrite the contents of file.

thus, if you use a connection in the file argument the value of the append argument is ignored.

simply specify the file argument as name of file:

cat(text, file="outputR.txt", append=TRUE, sep = "\n")

alternatively you can open the file connection with the correct mode specified

w+ - Open for reading and writing, truncating file initially.

fileConn <- file("outputR.txt", open = "w+")
for (i in 1:length(lines)){
  text <- paste("my text in line", i)
  cat(text, file = fileConn, sep = "\n")
}
close(fileConn)
wiep
  • 172
  • 1
  • 7
  • is it necessary to close the file when using cat? @wiep – Bioinformatician Oct 09 '16 at 10:23
  • if you have opened the file with the ```file``` function then you have to close the connection.if you use the file name in the ```cat``` function cat handles this for you. see the source https://github.com/wch/r-source/blob/trunk/src/library/base/R/cat.R – wiep Oct 09 '16 at 17:27
0

You have two options here:

1. Open the file in write mode:

lines <- c("aaaaa", "bbbb")


fileConn<-file("test.txt", "w")
for (i in 1:length(lines)){
  line = lines[i]
  cat(line, file=fileConn, append=TRUE, sep = "\n")
}
close(fileConn)

2 Use the write function with the append argument:

lines <- c("aaaaa", "bbbb")

for (i in 1:length(lines)){
  line = lines[i]
  write(line,file="test2",append=TRUE)  
}
USER_1
  • 2,409
  • 1
  • 28
  • 28
  • 1
    I already tried the second one, which gave me the same problem (overwriting) – Bioinformatician Oct 09 '16 at 11:26
  • That is very strange. The fileConn<-file("test.txt", "w") does not work either? You should post the whole of your data, including the lines variable. – USER_1 Oct 09 '16 at 11:43