2

I thought that snippet will do the trick:

f <- file("append2me.txt") write(1:5, ncolumns = 5, f) write(6:10, ncolumns = 5, f, append = T) close(f) f <- file("append2me.txt") print(readLines(f)) 

But the result is:

[1] "6 7 8 9 10" 

Why is that?

1
  • Just to note, setting f as string works fine: f <- "append2me.txt" Commented Nov 22, 2022 at 11:59

1 Answer 1

1

It is about how we open the file:

f <- file("append2me.txt", open = "a") write(1:5, ncolumns = 5, file = f) write(6:10, ncolumns = 5, file = f) close(f) f1 <- file("append2me.txt", open = "r") readLines(con = f1) close(f1) # [1] "1 2 3 4 5" "6 7 8 9 10" 

write is just a wrapper for cat, from cat manuals, see:

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.