title: How to write output in a text file line by line while running a function using lapply?
body: <p>This is my function</p>
  
  <pre><code>Mc_bval&lt;-function(filename){
    cat &lt;- read.table(paste(cat_file, sep=""), header=TRUE)
    mag &lt;- cat$Magnitude
    mag &lt;- cat$Magnitude
    Mc_bootstrap &lt;- numeric(nbsample)
    for(i in 1:nbsample) Mc_bootstrap[i] &lt;- maxc(sample(mag, replace=TRUE),mbin)$Mc
    Mc_mean &lt;- mean(Mc_bootstrap, na.rm=TRUE)
    Mc_sd &lt;- sd(Mc_bootstrap, na.rm=TRUE)
    mag_b &lt;- mag[mag&gt;=Mc_mean]
    b &lt;- b.guten(mag_b, Mc_mean)
    return(list(c(b=b),Mc=Mc_mean,Mc_sd=Mc_sd))
    #return(c(b=b,Mc))
  }
  </code></pre>
  
  <p>Function is returning values,I want by reading each file name in the filelist.
  I want to write output in one line for each file including the filename
  This is the code i tried</p>
  
  <pre><code>filelist = list.files(pattern="*$.csv$")
  outfile &lt;- file("checkout.txt","w")
  #apply(random_data, 2, checkfun, fileConn=outfile)
  
  out = lapply(filelist,FUN = Mc_bval)
  close(outfile)
  </code></pre>
  
  <p>Output file will be somewhat looks like this</p>
  
  <pre><code>cata_extract1.csv 1.46 0.25 4.15 0.33
  cata_extract2.csv 1.44 0.35 4.25 0.23
  cata_extract3.csv 1.4  0.25 4.25 0.24
  cata_extract4.csv 1.34 0.85 4.25 0.23
  </code></pre>
  
  <p>Could you help me out?</p>