<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/"></div></div><div><a data-url="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/" data-counturl="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/" data-count="horizontal" data-text="BERT: a newcomer in the R Excel connection" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.thertrader.com/2016/11/30/bert-a-newcomer-in-the-r-excel-connection/"> R – The R Trader</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A few months ago a reader point me out this new way of connecting R and Excel. I don’t know for how long this has been around, but I never came across it and I’ve never seen any blog post or article about it. So I decided to write a post as the tool is really worth it and before anyone asks, I’m not related to the company in any way.</p>
<p>BERT stands for Basic Excel R Toolkit. It’s free (licensed under the GPL v2)  and it has been developed by Structured Data LLC. At the time of writing the current version of BERT is 1.07. More information can be found <a target="_blank" rel="nofollow" href="https://bert-toolkit.com/">here</a>. From a more technical perspective, BERT is designed to support running R functions from Excel spreadsheet cells. In Excel terms, it’s for writing User-Defined Functions (UDFs) in R.</p>
<p>In this post I’m not going to show you how R and Excel interact via BERT. There are very good tutorials  <a target="_blank" rel="nofollow" href="https://bert-toolkit.com/bert-quick-start">here</a>, <a target="_blank" rel="nofollow" href="https://bert-toolkit.com/bert-example-functions">here</a> and <a target="_blank" rel="nofollow" href="https://bert-toolkit.com/talking-to-excel-from-r">here</a>. Instead I want to show you how I used BERT to build a “control tower” for my trading.</p>
<p><strong>How do I use BERT?</strong></p>
<p>My trading signals are generated using a long list of R files but I need the flexibility of Excel to display results quickly and efficiently. As shown above BERT can do this for me but I also want to tailor the application to my needs. By combining the power of XML, VBA, R and BERT I can create a good looking yet powerful application in the form of an Excel file with minimum VBA code. Ultimately I have a single Excel file gathering all the necessary tasks to manage my portfolio: database update, signal generation, orders submission etc… My approach  could be broken down in the 3 steps below:</p>
<ol>
<li>Use XML to build user defined menus and buttons  in an Excel file.</li>
<li>The above menus and buttons are essentially calls to VBA functions.</li>
<li>Those VBA functions are wrapup around R functions defined using BERT.</li>
</ol>
<p>With this approach I can keep a clear distinction between the core of my code kept in R, SQL and Python and everything used to display and format results kept in Excel, VBA &amp; XML. In the next sections I present the prerequisite to developed such an approach and a step by step guide that explains how BERT could be used for simply passing data from R to Excel with minimal VBA code.</p>
<p><strong>Prerequisite</strong></p>
<p>1 – <em>Download and install BERT from this <a target="_blank" rel="nofollow" href="https://bert-toolkit.com/download-bert">link</a></em>. Once the installation has completed you should have a new Add-Ins menu in Excel with the buttons as shown below. This is how BERT materialized in Excel.</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/www.thertrader.com/wp-content/uploads/2016/11/picture1.png"><img sizes="(max-width: 334px) 100vw, 334px" alt="picture1" class="wp-image-1487" src="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/picture1-300x79.png?resize=334%2C88" data-recalc-dims="1"></a></p>
<p>2 – <em>Download and install Custom UI editor</em>: The Custom UI Editor allows to create user defined menus and buttons in Excel ribbon. A step by step procedure is available <a target="_blank" rel="nofollow" href="http://www.rondebruin.nl/win/s2/win001.htm">here</a>.</p>
<p><strong>Step by step guide</strong></p>
<p><em>1 – R Code: </em>The below R function is a very simple piece of code for illustration purposes only. It calculates and return the residuals from a linear regression. This is what we want to retrieve in Excel. Save this in a file called <strong>myRCode.R</strong> (any other name is fine) in a directory of your choice.</p>
<pre>
myFunction &lt;- function(){
 aa &lt;- rnorm(200)
 bb &lt;- rnorm(200)
 res &lt;- lm(aa~bb)$res
 return(res)
}
</pre>
<p><em>2 – functions.R in BERT</em>: From Excel select <strong>Add-Ins</strong> -> <strong>Home Directory</strong> and open the file called <strong>functions.R</strong>. In this file paste the following code. Make sure you insert the correct path.</p>
<pre>
source(&quot;D:\\myPath\\myRCode.R&quot;)
</pre>
<p>This is just sourcing into BERT the R file you created above. Then save and close the file <strong>functions.R. </strong>Should you want to make any change to the R file created in step 1 you will have to reload it using the BERT button “Reload Startup File” from the Add-Ins menu in Excel</p>
<p><em>3 – In Excel: </em>Create and save a file called myFile.xslm (any other name is fine). This is a macro-enabled file that you save in the directory of your choice. Once the file is saved close it.</p>
<p><em>4 – Open the file created above in Custom UI editor</em>:<em> </em>Once the file is open, paste the below code.</p>
<pre>
&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2009/07/customui&quot;>
 &lt;ribbon startFromScratch=&quot;false&quot;>
 &lt;tabs>
 &lt;tab id=&quot;RTrader&quot; label=&quot;RTrader&quot;>
 &lt;group id=&quot;myGroup&quot; label=&quot;My Group&quot;>
 &lt;button id=&quot;button1&quot; label=&quot;New Button&quot; size=&quot;large&quot; onAction=&quot;myRCode&quot; imageMso=&quot;Chart3DColumnChart&quot; />
 &lt;/group> 
 &lt;/tab>
 &lt;/tabs>
 &lt;/ribbon>
&lt;/customUI>
</pre>
<p>You should have something like this in the XML editor:</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/xmlCodePicture.png"><img sizes="(max-width: 313px) 100vw, 313px" alt="xmlcodepicture" class="wp-image-1513" src="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/xmlCodePicture-300x113.png?resize=313%2C118" data-recalc-dims="1"></a></p>
<p>Essentially this piece of XML code creates an additional menu (RTrader), a new group (My Group) and a user defined button (New Button) in the Excel ribbon. Once you’re done, open myFile.xslm in Excel and close the Custom UI Editor. You should see something like this.</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/picture2.png"><img sizes="(max-width: 321px) 100vw, 321px" alt="picture2" class="wp-image-1491" src="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/picture2-300x100.png?resize=321%2C107" data-recalc-dims="1"></a></p>
<p><em>5 – Open VBA editor</em>: In myFile.xlsm insert a new module. Paste the code below in the newly created module.</p>
<pre>
Sub myRCode(control As IRibbonControl)
   Dim a As Variant
   Dim theLength As Integer
   ThisWorkbook.Sheets(&quot;Sheet1&quot;).Range(&quot;B1:B10000&quot;).ClearContents 
   a = Application.Run(&quot;BERT.Call&quot;, &quot;myFunction&quot;) 
   theLength = UBound(a, 1) + 1 
   ThisWorkbook.Sheets(&quot;Sheet1&quot;).Range(&quot;B1:B&quot; &amp; theLength).Value = a 
End Sub 
</pre>
<p>This erases previous results in the worksheet prior to coping new ones.</p>
<p><em>6 – Click New Button</em>: Now go back to the spreadsheet and in the RTrader menu click the “New Button” button. You should see something like the below appearing.</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/www.thertrader.com/wp-content/uploads/2016/11/resultsExcelCapture.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="resultsexcelcapture" class="wp-image-1519" src="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2016/11/resultsExcelCapture-300x169.png?resize=300%2C169" data-recalc-dims="1"></a></p>
<p>You’re done!</p>
<p>The guide above is a very basic version of what can be achieved using BERT but it shows you how to combine the power of several specific tools to build your own custom application. From my perspective the interest of such an approach is the ability to glue together R and Excel obviously but also to include via XML (and batch) pieces of code from Python, SQL and more. This is exactly what I needed. Finally I would be curious to know if anyone has any experience with BERT?</p>
<p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/"></div></div><div><a data-url="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/" data-counturl="https://www.r-bloggers.com/bert-a-newcomer-in-the-r-excel-connection/" data-count="vertical" data-text="BERT: a newcomer in the R Excel connection" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.thertrader.com/2016/11/30/bert-a-newcomer-in-the-r-excel-connection/"> R – The R Trader</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Y0x-AHs0lUw"></a></div>