title: How to print R matrix from sprintf
body: <p>For the following matrix :</p>
  
  <pre><code>&gt; mat
               [,1]          [,2]          [,3]
  [1,] 8.326673e-17  5.551115e-17  5.551115e-17
  [2,] 2.775558e-17  3.677614e-16 -5.551115e-17
  [3,] 0.000000e+00 -2.775558e-17  5.551115e-17
  </code></pre>
  
  <p>Consider the following <code>sprintf</code>:</p>
  
  <pre><code>sprintf("proj: %s", mat)
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>print(sprintf("proj: %s", mat))
  [1] "proj: 8.32667268468867e-17"  "proj: 2.77555756156289e-17"
  [3] "proj: 0"                     "proj: 5.55111512312578e-17"
  [5] "proj: 3.67761376907083e-16"  "proj: -2.77555756156289e-17"
  [7] "proj: 5.55111512312578e-17"  "proj: -5.55111512312578e-17"
  [9] "proj: 5.55111512312578e-17"
  </code></pre>
  
  <p>What we want is the same output as just <code>mat</code> but with the </p>
  
  <blockquote>
    <p>"proj: "</p>
  </blockquote>
  
  <p>message prepended. Specifically:</p>
  
  <pre><code>proj:             [,1]          [,2]          [,3]
  [1,] 8.326673e-17  5.551115e-17  5.551115e-17
  [2,] 2.775558e-17  3.677614e-16 -5.551115e-17
  [3,] 0.000000e+00 -2.775558e-17  5.551115e-17
  </code></pre>
  
  <p>How to use sprintf to achieve that?</p>
  
  <p><strong>UPDATE</strong>  From answer from Sven an attempt to create a function is here:</p>
  
  <pre><code>printmat &lt;- function(mat) {
    out &lt;- capture.output(mat)
    out[1] &lt;- paste0("proj:", out[1])
    paste(out, collapse = "\n")
  }
  </code></pre>
  
  <p>Here is output: single line (would be better 4 lines like original):</p>
  
  <pre><code>print(printmat(mat))
  
  [1] "proj:             [,1]          [,2]          [,3]\n[1,] 8.326673e-17  5.551115e-17  5.551115e-17\n[2,] 2.775558e-17  3.677614e-16 -5.551115e-17\n[3,] 0.000000e+00 -2.775558e-17  5.551115e-17"
  </code></pre>
  
  <p><strong>Another update</strong>  A different answer - from Richard Scriven - uses <code>print.listof(list)</code>.  Here is a function incorporating the <code>capture.output</code>.  This solution however suffers from not allowing flexible message: it is hardcoded to <code>"proj"</code>:</p>
  
  <pre><code>printmat &lt;- function(mat) {
    out &lt;- capture.output(print.listof(list(proj = mat)))
  }
  </code></pre>
  
  <p>Here is the output: it is <em>close</em> .. but notice we do not have choice of changing <code>"proj"</code> to something else:</p>
  
  <pre><code>   print(printmat(mat))
  
  [1] "proj :"
  [2] "             [,1]          [,2]          [,3]"
  [3] "[1,] 8.326673e-17  5.551115e-17  5.551115e-17"
  [4] "[2,] 2.775558e-17  3.677614e-16 -5.551115e-17"
  [5] "[3,] 0.000000e+00 -2.775558e-17  5.551115e-17"
  [6] ""
  </code></pre>
  
  <p><strong>Third update</strong>   I have added a flexible "message/matrix name" parameter. However <code>sprintf</code> prepends it to EVERY line of the output:</p>
  
  <pre><code>printmat &lt;- function(msg, mat) {
    out &lt;- capture.output(print.listof(list(mat)))
    sprintf("%s: %s", msg, out)
  }
  </code></pre>
  
  <p>Here is a test:</p>
  
  <pre><code>&gt; print(printmat("mymat", mat))
  [1] "mymat: Component 1 :"
  [2] "mymat:              [,1]          [,2]          [,3]"
  [3] "mymat: [1,] 8.326673e-17  5.551115e-17  5.551115e-17"
  [4] "mymat: [2,] 2.775558e-17  3.677614e-16 -5.551115e-17"
  [5] "mymat: [3,] 0.000000e+00 -2.775558e-17  5.551115e-17"
  [6] "mymat:
  </code></pre>
  
  <p>So is there a way to suppress the <strong><code>sprintf</code></strong> from prefixing <em>every</em> line with the message?</p>