title: How to create a text file in R to output results from different variables?
body: <p>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 <code>sink</code> and <code>cat</code>. The problem is that I have to create a <code>cat</code> for each independent line. Is there a way to write a big paragraph without using <code>cat</code> at each line? </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>The output looks like this: </p>
  
  <pre><code>[1] "===============================================================  \n\nNEW MODEL  \n===============================================================  \nSummary of the model:"
   [1]  1  2  3  4  5  6  7  8  9 10
  </code></pre>
  
  <p>But I would prefer to have something like this: </p>
  
  <pre><code>===============================================================
  NEW MODEL
  ===============================================================
  
  Summary of the model:
  1  2  3  4  5  6  7  8  9 10
  </code></pre>
  
  <p>You can write this (but is there a way not to write cat at each line?): </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>To get this: </p>
  
  <pre><code>===============================================================
  NEW MODEL
  ===============================================================
  Summary of the model:
   [1]  1  2  3  4  5  6  7  8  9 10
  # model.summary$BUGSoutput$sims.list
  </code></pre>
  
  <p>But interestingly, this is not working: </p>
  
  <pre><code>yo &lt;- function(x) {
    sink("~/Desktop/potato.txt", type="output")
    writeLines("===============================================================
  NEW MODEL
  ===============================================================
  Summary of the model:")
  x
  # other stuff
    sink()
  
  }
  
  yo(1:10)
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>===============================================================
  NEW MODEL
  ===============================================================
  Summary of the model:
  </code></pre>