I'm wondering how can I export a text file from an R script. I want to preset some text to be printed regardless of the results, but I also want to add variables that could change in my text file. The only way I know how to do this is by using sink and cat. The problem is that I have to create a cat for each independent line. Is there a way to write a big paragraph without using cat at each line?
x = 1:10
sink("~/Desktop/TEST.txt", type=c("output", "message"), append = FALSE)
"=============================================================== \n
NEW MODEL
===============================================================
Summary of the model:"
x
# model.summary$BUGSoutput$sims.list
sink(NULL)
The output looks like this:
[1] "=============================================================== \n\nNEW MODEL \n=============================================================== \nSummary of the model:"
[1] 1 2 3 4 5 6 7 8 9 10
But I would prefer to have something like this:
===============================================================
NEW MODEL
===============================================================
Summary of the model:
1 2 3 4 5 6 7 8 9 10
You can write this (but is there a way not to write cat at each line?):
x = 1:10
sink("~/Desktop/TEST.txt", type=c("output", "message"), append = FALSE)
cat("===============================================================\n")
cat("NEW MODEL\n")
cat("===============================================================\n")
cat("Summary of the model:\n")
x
cat("# model.summary$BUGSoutput$sims.list\n")
sink(NULL)
To get this:
===============================================================
NEW MODEL
===============================================================
Summary of the model:
[1] 1 2 3 4 5 6 7 8 9 10
# model.summary$BUGSoutput$sims.list
But interestingly, this is not working:
yo <- function(x) {
sink("~/Desktop/potato.txt", type="output")
writeLines("===============================================================
NEW MODEL
===============================================================
Summary of the model:")
x
# other stuff
sink()
}
yo(1:10)
Output:
===============================================================
NEW MODEL
===============================================================
Summary of the model: