Search code examples
rsink

Why isn't the R function sink() writing a summary output to my results file?


Edit: This problem doesn't seem to be reproducible at this point, but I've updated this question to a more concise example that illustrates what the behavior was, in case anyone encounters a similar issue.

sink("res4.txt")
    cat("Here are my results:\n")
    summary(mtcars)
sink()

The sink("~/R/res4.txt") function in the last line will store the "Here are my results" line, but not the summary(res4) line in the .txt file.

Typing summary(mtcars) produces the correct data set, and I don't understand why the output of summary(mtcars) isn't included.


Solution

  • There are as many summary functions as there are regression procedures and many of them use cat with would not get into a value returned. My suggestion is to use cat and capture.output both of which have a file destination parameter and an append option:

    cat("Here are my results:\n", file="~/R/res4.txt")
    capture.output( summary(res4), file"~/R/res4.txt", append=TRUE)