<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/windows-clipboard-access-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-bar.net/r-clipboard-data-copy-paste/"> R – R-BAR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="Two tables form spreadsheet to R via clipboard" src="https://i0.wp.com/cdn.r-bar.net/PID545/clipboard_r.gif?w=456" data-recalc-dims="1"><br>
The windows clipboard is a quick way to get data in and out of R. How can we exploit this feature to accomplish our basic data exploration needs and when might its use be inappropriate? Read on. </p>
<h2>Overview</h2>
<ol>
<li><a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/#Paste_Data_from_the_Clipboard_into_R" target="_blank">Paste Data from Clipboard to R</a>
</li>
<ol>
<li>
<a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/#Paste_from_HTML_Tables" target="_blank">HTML Tables</a></li>
<li>
<a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/#Paste_From_Spreadsheets" target="_blank">Spreadsheets</a></li>
</ol>
<li>
<a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/#Copy_Data_Back_to_the_Clipboard_from_R" target="_blank">Copy Data from R to Clipboard</a></li>
<li><a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/#Summary_and_Usage_Notes" target="_blank">Summary and Usage Notes</a></li>
</ol>
<h1>Paste Data from Clipboard to R</h1>
<p>We won't be using it in this post, but you can see the contents of the Windows clipboard in R using the readClipboard() command. Going through the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/utils/html/clipboard.html" target="_blank">documentation</a> you'll note a variety of formats that can be read. For our purposes, we are looking at moving text based data into R from HTML tables or spreadsheets that happen to be open on our desktop. </p>
<h2>HTML Tables</h2>
<p>HTML tables are easy to copy into R via the windows clipboard if you're using Chrome or Firefox. To demonstrate, copy the simple HTML table shown below by highlighting the text and pressing CTRL + C on your keyboard (or whatever copy method works best for you.) </p>
<table rules="ALL">
<thead>
<tr>
<th> Items </th>
<th> Inventory </th>
</tr>
</thead>
<tbody>
<tr>
<td> Apple </td>
<td> 5 </td>
</tr>
<tr>
<td> Kiwi </td>
<td> 7 </td>
</tr>
<tr>
<td> Meat Balls </td>
<td> 253 </td>
</tr>
</tbody>
</table>
<p>Next, grab the windows clipboard content and “paste” it into an R data frame using the following R-code:</p>
<div><table><tr><td><pre>1
2
3
4
</pre></td><td><pre>My_HTML_Data <span>&lt;-</span> 
  <span>read.<span>table</span></span><span>(</span><span>file</span> <span>=</span> <span>&quot;clipboard&quot;</span>, 
             header <span>=</span> <span>T</span>, 
             sep <span>=</span> <span>&quot;<span>\t</span>&quot;</span><span>)</span></pre></td></tr></table></div>
<p>Notice that the file argument of the read.table command is calling out to the windows “clipboard” (line 2). </p>
<p><strong>Note 1:</strong> I've gotten this to work with Mozilla Firefox and Google Chrome but <strong>Not</strong> Microsoft Edge or Internet Explorer.<br>
<strong>Note 2:</strong> You copied all the data, but you might have gotten a warning that looks like this:</p>
<blockquote>
<p>
Warning message:<br>
In read.table(file = “clipboard”, header = T, sep = “\t”) :<br>
incomplete final line found by readTableHeader on ‘clipboard’
</p>
</blockquote>
<p>There seems to be a hidden character at the end of the HTML table. If you miss it, you get the error mentioned above. Digging deeper into the warning with the readClipboard command, you get something that looks like this:</p>
<div><table><tr><td><pre>1
2
</pre></td><td><pre>readClipboard<span>(</span><span>)</span>
<span>## [1] &quot;Items \tInventory&quot; &quot;Apple \t5&quot; &quot;Kiwi \t7&quot; &quot;Meat Balls \t253 &quot;</span></pre></td></tr></table></div>
<p>If you get the hidden character, you don't get the error. Running the readClipboard command show there is an extra empty item in the list (see line 3):</p>
<div><table><tr><td><pre>1
2
3
</pre></td><td><pre>readClipboard<span>(</span><span>)</span>
<span>##[1] &quot;Items \tInventory&quot; &quot;Apple \t5&quot; &quot;Kiwi \t7&quot; &quot;Meat Balls \t253&quot; </span>
<span>##[5] &quot;&quot;</span></pre></td></tr></table></div>
<p>Either way, all the data ends up in your target data frame. </p>
<h2>Spreadsheets</h2>
<p>I can think of numerous times when I've had multiple small tables in the same spreadsheet as shown below. </p>
<p><img alt="Two tables form spreadsheet to R via clipboard" src="https://i0.wp.com/cdn.r-bar.net/PID545/two_tables_to_clipboard.gif?w=456" data-recalc-dims="1"></p>
<p>In these example tables, we see data related to notable serial killers. The left table provides the Name and Sex of a given serial killer. The right table provides their year of Birth and Death as well as the number of victims. Both tables are in the same spreadsheet.</p>
<p>Often, I want to merge or perform a quick analysis on tables like these in R but saving each table to a csv first is a nuisance. To get around this problem, we can leverage the Window's clipboard using the following code:</p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td><pre><span>#Copy the Left table from Spread Sheet</span>
Left_Table  <span>&lt;-</span> 
  <span>read.<span>table</span></span><span>(</span><span>file</span> <span>=</span> <span>&quot;clipboard&quot;</span>, 
             header <span>=</span> <span>T</span>, 
             sep <span>=</span> <span>&quot;<span>\t</span>&quot;</span><span>)</span>
 
<span>#Copy the Right table from Spread Sheet</span>
Right_Table  <span>&lt;-</span> 
  <span>read.<span>table</span></span><span>(</span><span>file</span> <span>=</span> <span>&quot;clipboard&quot;</span>, 
             header <span>=</span> <span>T</span>, 
             sep <span>=</span> <span>&quot;<span>\t</span>&quot;</span><span>)</span></pre></td></tr></table></div>
<p><strong>Note:</strong> Between each read.table command, you will need to copy the appropriate data from the target spreadsheet to the clipboard. Once you have the tables in R, you can use the write.csv command to write them to disk.</p>
<h1>Copy Data from R to Clipboard</h1>
<p>In the previous section, we copied two tables containing information about serial killers into R. In this section, we are going to merge those tables together and copy the result back to our spreadsheet. The code to merge and copy to clipboard is shown below: </p>
<div><table><tr><td><pre>1
2
3
4
</pre></td><td><pre>Merged_TBLs <span>&lt;-</span> 
  <span>merge</span><span>(</span>Left_Table, Right_Table, 
        <span>by</span><span>=</span><span>&quot;KEY&quot;</span>, all.<span>x</span> <span>=</span> <span>T</span><span>)</span>
<span>write.<span>table</span></span><span>(</span>Merged_TBLs, <span>file</span><span>=</span><span>&quot;clipboard&quot;</span>, <span>row.<span>names</span></span> <span>=</span> <span>F</span><span>)</span></pre></td></tr></table></div>
<p>Here is a screenshot showing the merged table pasted back into our spreadsheet software:</p>
<p><img alt="Merged tables from R to spreadsheet via clipboard" src="https://i0.wp.com/cdn.r-bar.net/PID545/merged_table_from_R_clipboard.gif?w=456" data-recalc-dims="1"></p>
<p><strong>Note:</strong> after pasting the table back into the spreadsheet, a <a rel="nofollow" href="https://www.excel-easy.com/examples/text-to-columns.html" target="_blank">text-to-column</a> process was required to put the data back into the individual columns. </p>
<h1>Summary and Usage Notes</h1>
<p>Copying and pasting data to and from the Window's clipboard to R is quick and easy. All we need to do is use the standard read.table and write.table syntax and set the file argument equal to “clipboard”. Despite the ease of moving data around in this manner, we are straying away from reproducible data processing. For example, it would be hard for me to reproduce your result if your R code referenced the clipboard. With this in mind, get your source data into a csv file if your quick glace at HTML or spreadsheet data becomes more than a 30 minute affair or you want to share you analysis. Finally, if your running a OS X or X11 based windowing systems check out the <a rel="nofollow" href="https://cran.r-project.org/web/packages/clipr/index.html" target="_blank">clipr package</a>.</p>
<hr>
<p>Other Articles at r-bar.net: <a rel="nofollow" href="http://r-bar.net/control-charts-with-ggqc-xmr/" target="_blank">XmR Plots with ggQC</a></p>
<p>The post <a rel="nofollow" href="http://r-bar.net/r-clipboard-data-copy-paste/" target="_blank">Windows Clipboard Access with R</a> appeared first on <a rel="nofollow" href="http://r-bar.net/" target="_blank">R-BAR</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-bar.net/r-clipboard-data-copy-paste/"> R – R-BAR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/iG8DzT39WGg" height="1"></a></div>