title: R: rvest extracting innerHTML
body: <p>Using <strong>rvest</strong> in R to scrape a web-page, I'd like to <strong>extract the equivalent of <code>innerHTML</code> from a node</strong>, in particular to change line-breaks into newlines before applying <code>html_text</code>.</p>
  
  <p>Example of desired functionality:</p>
  
  <pre><code>library(rvest)
  doc &lt;- read_html('&lt;html&gt;&lt;p class="pp"&gt;First Line&lt;br /&gt;Second Line&lt;/p&gt;')
  innerHTML(doc, ".pp")
  </code></pre>
  
  <p>Shall produce following output:</p>
  
  <pre><code>[1] "&lt;p class=\"pp\"&gt;First Line&lt;br&gt;Second Line&lt;/p&gt;"
  </code></pre>
  
  <p>With <code>rvest 0.2</code> this can be achieved through <strong><code>toString.XMLNode</code></strong></p>
  
  <pre><code># run under rvest 0.2
  library(XML)
  html('&lt;html&gt;&lt;p class="pp"&gt;First Line&lt;br /&gt;Second Line&lt;/p&gt;') %&gt;% 
    html_node(".pp") %&gt;% 
    toString.XMLNode
  [1] "&lt;p class=\"pp\"&gt;First Line&lt;br&gt;Second Line&lt;/p&gt;"
  </code></pre>
  
  <p>With the newer <code>rvest 0.2.0.900</code> this does not work anymore.</p>
  
  <pre><code># run under rvest 0.2.0.900
  library(XML)
  html_node(doc,".pp") %&gt;% 
    toString.XMLNode
  [1] "{xml_node}\n&lt;p&gt;\n[1] &lt;br/&gt;"
  </code></pre>
  
  <p>The desired functionality is generally available in the <strong><code>write_xml</code></strong> function of package <code>xml2</code> on which <code>rvest</code> now depends - if only <code>write_xml</code> could give its output to a variable instead of insisting to write to a file. (also a <code>textConnection</code> is not accepted).</p>
  
  <p>As a <em>workaround</em> I can temporarily write to a file:</p>
  
  <pre><code># extract innerHTML, workaround: write/read to/from temp file
  html_innerHTML &lt;- function(x, css, xpath) {
    file &lt;- tempfile()
    html_node(x,css) %&gt;% write_xml(file)
    txt &lt;- readLines(file, warn=FALSE)
    unlink(file)
    txt
  }
  html_innerHTML(doc, ".pp") 
  [1] "&lt;p class=\"pp\"&gt;First Line&lt;br&gt;Second Line&lt;/p&gt;"
  </code></pre>
  
  <p>with this I can then for example transform the line break tags into new-line characters:</p>
  
  <pre><code>html_innerHTML(doc, ".pp") %&gt;% 
    gsub("&lt;br\\s*/?\\s*&gt;","\n", .) %&gt;%
    read_html %&gt;%
    html_text
  [1] "First Line\nSecond Line"
  </code></pre>
  
  <p>Is there a better way to do this with existing functions from e.g. <code>rvest</code>, <code>xml2</code>, <code>XML</code> or other packages? In particular I'd like to avoid to write to the hard disk.</p>