title: Writing to one file from different methods in R
body: <p>In general, I'm trying to create R code that reads questions from different txt files (test1.txt, test2.txt,...) and creates one file (exam.txt). In other words, I have different questions pool files and I'd like to create one exam file out of them. Type of questions (true/false, Multiple-choices, short-answer question, fill in he spaces, table matching ).  The reason I chose to go with txt file because MS docx files are not supported by R ( unless I go through long process  <a href="http://www.r-statistics.com/2013/03/write-ms-word-document-using-r-with-as-little-overhead-as-possible/" rel="nofollow">as pointed in this blog</a>  )</p>
  
  <p>I found <a href="http://cran.r-project.org/web/packages/exams/index.html" rel="nofollow">exams package</a> to create exams, which is not exactly what I want.</p>
  
  <p>A simple approach would be reading files one by one and dump the questions in a single file(exam.txt). I'm facing two problems:</p>
  
  <p>1-How to write the already read contents to a file (exam.txt -Note: txt or csv or xlsx is fine, however, I prefer txt for simplicity) and keep it open for the next file contents.</p>
  
  <p>2- How to choose how many questions to read from each file.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>ReadTxt&lt;-function(){
  fileName="test1.txt"
  conn=file(fileName,open="r",encoding = 'UTF-8')
  linn=readLines(conn)
  for (i in 1:length(linn)){
     if (!(grepl("#",linn[i])) )
     cat(linn[i],"\n")
  }
  close(conn)
  }
  </code></pre>
  
  <p>I have used <code>#</code> as a separator between question in each txt file. That's why I'm using grepl command.</p>
  
  <p>sample of <code>test1.txt:</code></p>
  
  <pre><code>First question goes here
  #
  Second question
  #
  Third question 
  #
  </code></pre>
  
  <p>Full R code would be something like this:</p>
  
  <pre><code>createExam&lt;-function(){ 
  ReadTxt("test1.txt")
  #ReadTxt("test2.txt")
  #ReadTxt("test3.txt")
   }
  
  ReadTxt&lt;-function(fname){
  fileName=fname
  conn=file(fileName,open="r",encoding = 'UTF-8')
  linn=readLines(conn)
  for (i in 1:length(linn)){
    if (!(grepl("#",linn[i])) )
    cat(linn[i],"\n")
   }
   close(conn)
  
  
   }
  </code></pre>