1

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:
1
  • You might want to look into using knitR with say AsciiDoc or Markdown output ... if you don't use the fancier features in those you should end up with essentially plain text Commented Sep 11, 2016 at 5:29

1 Answer 1

1

Use ?writeLines. Consider:

sink(<file name>, type="output")
writeLines("===============================================================
NEW MODEL
===============================================================
Summary of the model:")
summary(model)
# other stuff
sink()
Sign up to request clarification or add additional context in comments.

2 Comments

That seems like a different question, @M.Beausoleil. I'm not really sure what you mean. You'll need a reproducible example to illustrate the situation you have in mind.
I have no idea. Using print(x) inside the sink in the function seems to work. But when sink isn't in the function, you don't seem to need it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.