title: Sink inside a For-Loop gives the error "sink stack is full" in R
body: <p>I am working with the following data in R:</p>
  
  <pre><code>&gt; str(df)
  'data.frame':   369269 obs. of  12 variables:
   $ bkod : int  110006 110006 110006 110006 110006 110006 110006 110006 110006 110006 ...
   $ bad  : Factor w/ 215 levels "4. Levent","500 Evler",..: 26 26 26 26 26 26 26 26 26 26 ...
   $ mkod : int  359 359 359 359 359 359 359 359 359 359 ...
   $ mad  : Factor w/ 8643 levels "    Hilal Gida           ",..: 4021 4021 4021 4021 4021 4021 4021 4021 4021 4021 ...
   $ yekod: int  12 12 12 12 12 12 12 12 12 12 ...
   $ yad  : Factor w/ 44 levels "BAKUGAN","BARBIE",..: 1 1 1 1 1 2 2 2 2 2 ...
   $ donem: int  201101 201103 201105 201107 201109 201102 201103 201104 201106 201107 ...
   $ sayi : int  201101 201102 201103 201104 201105 201102 201103 201104 201106 201107 ...
   $ plan : num  5 3 2 7 5 0 2 2 0 2 ...
   $ sevk : int  5 6 4 7 6 3 2 5 5 2 ...
   $ iade : int  4 1 3 5 5 2 2 5 0 2 ...
   $ satis: int  1 5 1 2 1 1 0 0 5 0 ...
  
  bkv &lt;- unique(df$bkod)
  &gt; bkv
    [1] 110006 110007 110010 110011 110020 110022 110027 110030 110032 110036 110043 110049 110051 110054 110056 110061 110062 110067 110069 110071 110077
   [22] 110084 110092 110093 110094 110100 110101 110104 110109 110111 110113 110116 110117 110120 110133 110138 110143 110158 110160 110162 110186 110187
   [43] 110198 110202 110208 110213 110214 110216 110218 110219 110222 110224 110229 110232 110234 110235 110247 110249 110250 110252 110255 110256 110262
   [64] 110265 110266 110268 110269 110270 110274 110277 110278 110282 110285 110295 110296 110301 110306 110309 110310 110311 110313 110314 110315 110319
   [85] 110329 110346 110351 110354 110357 110359 110377 110379 110380 110386 110392 110394 110396 110397 110400 110406 110412 110413 110419 110421 110422
  [106] 110423 110426 110428 110431 110439 110442 110444 110452 110455 110464 110467 110469 110470 110471 110475 110478 110480 110485 110490 110492 110498
  [127] 110499 110503 110505 110506 110508 110509 110512 110514 110517 110518 110519 110523 110532 110534 110537 110540 110543 110544 110545 110546 110547
  [148] 110549 110550 110551 110553 110554 110555 110556 110557 110559 110560 110562 110565 110566 110569 110570 110571 110572 110573 110575 110576 110577
  [169] 110578 110579 110580 110581 110582 110583 110584 110586 110587 110588 110589 110590 110591 110594 110595 110596 110597 110598 110599 110600 110602
  [190] 110603 110605 110606 110607 110608 110609 110610 110611 110612 110613 110614 110615 110616 110617 110619 110620 110621 110622 110623 110624 110625
  [211] 110626 110627 110628 110629 110630
  </code></pre>
  
  <p>What I want to achieve is to make some calculations with respect to each element in <code>bkv</code> and append the results to a txt file using <code>sink</code> inside a For-Loop. So I created the following loop;</p>
  
  <pre><code>for (i in bkv) {
    new &lt;- df[df$bkod==i,]
    dp &lt;- new[new$plan!=new$sevk,]
    medy &lt;- 100*(nrow(dp)/nrow(new))
    sink("Report.txt")
    cat(i, "için müdahale oranı: %", medy, "\n")
  }
  </code></pre>
  
  <p>but it gives the following error:</p>
  
  <blockquote>
    <p>Error in sink("Report.txt") : sink stack is full</p>
  </blockquote>
  
  <p>It creates Report.txt but it's an empty file. The report should have 215 lines as <code>bkv</code> has 215 elements.</p>
  
  <p>What am I doing wrong? What should I do to make this loop work correctly and append the results to the text file?</p>