5

I ran these two code blocks, expecting the same output

cattest <- file("cattest.txt")
cat("First thing", file = cattest)
cat("Second thing", file = cattest, append = TRUE)
close(cattest)

sink("cattest_sink.txt")
cat("First thing")
cat("Second thing")
sink()

But the resulting cattest.txt contains only "Second thing", whereas the cattest_sink.txt includes what I expected, "First thingSecond thing". Why is the append argument ignored with the file connection?

I'm on 64bit R 3.0.1 on Windows, in case it matters.

4

2 回答 2

6

因为这就是说如果不是文件名?cat它将执行的操作。file

附加:逻辑。仅当参数 'file' 是文件名(而不是连接或 '"|cmd"')时才使用。如果“TRUE”输出将附加到“文件”;否则,它将覆盖“文件”的内容。

于 2013-07-17T21:23:42.907 回答
5

使用追加文本的一种方法cat是打开 mode 的文件连接a

cattest <- file("cattest.txt")
cat("First thing", file = cattest, fill = TRUE)
close(cattest)

cattest <- file("cattest.txt", open = "a")
cat("Second thing", file = cattest)
close(cattest)
于 2013-07-18T13:28:56.787 回答