<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/using-r-with-emacs-and-ess/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r.prevos.net/using-r-with-emacs/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A few years ago I ditched the spreadsheet in favour of writing code in R. During the process I learnt a valuable lesson: The steeper the learning curve, the larger the reward. My time invested in learning R has paid off in spades, and I now use the R language for all my numerical and <a rel="nofollow" href="https://r.prevos.net/qualitative-data-science/" target="_blank">qualitative analysis</a>.</p>
<p>Most data scientists solve data problems with the R language using the <a rel="nofollow" href="https://www.rstudio.com/" target="_blank">RStudio</a> IDE, a free and open-source Integrated Development Environment. Although RStudio is a great product, like all other software products, it’s functionality is limited to doing one thing well.</p>
<p>Since last year I use <a rel="nofollow" href="https://www.gnu.org/software/emacs/" target="_blank">Emacs</a> and once again the rule that a steep learning curve has a valuable reward has come true. Emacs is the Swiss army chainsaw of productivity, the ultimate killer app and also one of the <a rel="nofollow" href="https://www.emacswiki.org/emacs/EmacsHistory" target="_blank">oldest</a> active pieces of software. Although it might seem that newer is better when it comes to software, Emacs has continuously evolved. The power of Emacs is its extensive functionality and virtually infinite customisability. Almost ninety per cent of all my computing activity now takes place within Emacs. I use it to write notes, manage my action lists, write e-mails, articles, books. The <a rel="nofollow" href="https://orgmode.org/" target="_blank">Org Mode</a> package in Emacs is the working horse that undertakes many of these functions. Since recently also to write R code in Emacs.</p>
<p>To master Emacs to the level, I am at now has taken me a couple of months. Initially, the bewildering amount of keyboard shortcuts is a challenge, but your muscle memory will soon kick in, and your fingers will glide across the keyboard like an eagle.</p>
<div class="wp-caption"><a rel="nofollow" href="https://r.prevos.net/using-r-with-emacs/emacs/" target="_blank"><img data-orig-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/06/emacs.png?fit=1859%2C1053&amp;ssl=1" data-orig-size="1859,1053" data-medium-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/06/emacs.png?fit=300%2C170&amp;ssl=1" data-attachment-id="1411" data-image-meta="{" data-image-description="&lt;p&gt;Using R with Emacs and ESS: Org Mode with embeded R code to write an academic paper in APA format.&lt;/p&gt;
" src="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/06/emacs.png?resize=584%2C331&ssl=1" data-permalink="https://r.prevos.net/using-r-with-emacs/emacs/" data-large-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/06/emacs.png?fit=584%2C331&amp;ssl=1" alt="Using R with Emacs and ESS: Org Mode with embeded R code to write an academic paper in APA format." data-image-title="Using R with Emacs and ESS: Org Mode with embeded R code to write an academic paper in APA format." sizes="(max-width: 584px) 100vw, 584px" width="450" data-comments-opened="1" class="wp-image-1411" data-recalc-dims="1"></a>
<p class="wp-caption-text">Using R with Emacs and ESS: Org Mode with embedded R code to write an academic paper in APA format.</p>
</div>
<h2>Using R with Emacs and ESS</h2>
<p>The advantage of using Emacs over RStudio is that I can seamlessly switch between my notes, to-do lists, calendars and so on, while also developing code. There is no need to install, maintain and master multiple applications as Emacs cover almost all my computing needs. Just like R, Emacs includes thousands of packages that extend its functionality far beyond what one would expect from a text editor.</p>
<p>The disadvantage of Emacs compared to RStudio is that it is not as pretty and the screen more resembles an angry fruit salad than a slick material design. The image above shows my screen when I translated a paper about body image research into Org Mode with embedded R code. A temporary disadvantage is that there is a bit of preparation is required to enable coding in Emacs, but that is only a temporary hurdle and adds to the learning curve.</p>
<p>This article explains how to start Using R with Emacs and ESS (<a rel="nofollow" href="https://ess.r-project.org/" target="_blank">Emacs Speaks Statistics</a>). The first section of this article provides links to resources on how to install Emacs on various platforms and how to enable it to start writing code in R with the ESS package. The second sections show how to use the R console and source files. The last section introduces literate programming by combining Org Mode and R and exporting the results to PDF, HTML etc.</p>
<h2>Getting Started with Emacs</h2>
<p>Installing Emacs depends on your operating system. A simple Google search will tell you what to do. The video playlist below shows how to install Emacs on Ubuntu, OS X or Windows 10. You will also need to install the <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R language</a> on your machine, and if you like to create publication-quality output, the also include <a rel="nofollow" href="https://www.latex-project.org/" target="_blank">LaTeX</a>.</p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/videoseries?list=PLGx4TR_jvOQAmztGFrV7SO6vTjSaiXTUo&hl=en_US" width="450" type="text/html"></iframe></p>
<p>Emacs needs an <a rel="nofollow" href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html" target="_blank">init file</a> to define the packages that need to be loaded and to define several settings. A minimal-working init file can be downloaded from my <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Miscellaneous/BodyImage" target="_blank">GitHub repository</a>.</p>
<p>To install packages, type <code>M-x list-packages</code>. This function displays all available packages. In Emacs-speak this means pressing the Alt (modify) key and the lowercase ‘x’ key, followed by ‘list-packages’ and return. You can find ESS by pressing ‘f’ and search for the package. Type ‘i’ to mark it for installation and press ‘x’ to execute the installation. The example init file loads the following packages that need to be manually installed:</p>
<ul>
<li><a rel="nofollow" href="https://ess.r-project.org/" target="_blank">ESS</a>: Emacs Speaks Statistics.</li>
<li><a rel="nofollow" href="https://github.com/mattfidler/ess-smart-underscore.el" target="_blank">ESS Smart Underscore</a>: <span>Smarter underscore for Emacs Speaks Statistics.</span></li>
<li><a rel="nofollow" href="https://github.com/jkitchin/org-ref" target="_blank">Org-Ref</a>: <span>org-mode modules for citations, cross-references, bibliographies in org-mode and useful BibTeX tools to go with it. </span></li>
<li><a rel="nofollow" href="https://github.com/auto-complete/auto-complete" target="_blank">auto-complete</a>: Auto Completion for GNU Emacs.</li>
</ul>
<h2>Using R with Emacs and ESS</h2>
<p>To start using R with Emacs type <code>M-x R RET</code>. ESS will ask to nominate a working directory and opens a new buffer with the name <code>*R*</code>. You can now use the R console to write code as you are used to. You can open multiple instances of the R console with different working directories and environment, just like projects in RStudio.</p>
<p>Any file with the .R extension is now recognised as being written in the R language. This is a so-called Emacs major mode, which is the key functionality of Emacs. A <a rel="nofollow" href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Modes.html" target="_blank">major mode</a> defines how a file is managed, whether it is an R file, an Org Mode file and so on.</p>
<p>One bit of useful information is that the underscore key is mapped to the <code>&lt;-</code> assignment<a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/assignOps.html" target="_blank"> operator</a>. If you need an underscore, you need to type it twice in a row. This can be annoying when you are an avid user of ggplot. The <a rel="nofollow" href="https://github.com/mattfidler/ess-smart-underscore.el" target="_blank">ess-smart-underscore</a> package solves this issue by extending the functionality of ESS. You can install this package the same way you installed ESS itself. To enable it, add <code>(require 'ess-smart-underscore)</code> to your init file.</p>
<p>To write in a source file, you can create a .R file by typing <code>C-x C-f</code> (<a rel="nofollow" href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Visiting.html" target="_blank">find-file</a> function). Type the filename to create a new file, or when it already exists, open the file. You can now start writing R code as you would in any other editor. As soon as you evaluate code for the first time in a session, ESS will ask you what the starting project directory is, which is defaulted to the folder that your .R file is in. To evaluate the whole buffer, use C-c B, to evaluate a section use C-c c and to evaluate a line press M-RET.</p>
<p>To show the source file and the R console next to each other, type <code>C-x 3</code> to <a rel="nofollow" href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Split-Window.html" target="_blank">split your window</a> to show two buffers. You can then use C-x b to select the other buffer.</p>
<p>A disadvantage of using R in ESS is that there is no simple way to integrate plot outputs into the Emacs window. When I am iteratively working on a visualisation, I save it to a file and open it in a separate buffer, as shown in the screendump.</p>
<h2>Using R with Emacs and ESS to Write an Academic Paper</h2>
<p>Org Mode is the most popular extension of Emacs and is precompiled with current versions of the software. Org Mode is an extremely versatile text editing extension. I use it to manage my projects using a calendar and To-Do lists, I write notes, write books an articles. I have translated an article I previously wrote <a rel="nofollow" href="https://r.prevos.net/r-sweave-and-latex/" target="_blank">LaTeX and Sweave</a> to Org Mode.</p>
<p>Org Mode works very well with <a rel="nofollow" href="https://orgmode.org/manual/LaTeX-export.html" target="_blank">LaTeX</a>. You will need to write some code into your init file to set a template, after which writing LaTeX code is a breeze. The <a rel="nofollow" href="https://orgmode.org/worg/org-contrib/babel/" target="_blank">Org Babel</a> package functions as an interface between Org Mode and R. Perhaps explaining this file in detail is a topic for a future post. You can view the most recent version of the Org File and the associated setting in the init file on my <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Miscellaneous/BodyImage" target="_blank">GitHub page</a>.</p>
<div class="wp-caption"><a rel="nofollow" href="https://r.prevos.net/r-sweave-and-latex/openbook_1000x647/" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=996%2C384&amp;ssl=1" data-orig-size="996,384" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=300%2C116&amp;ssl=1" data-attachment-id="1022" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?resize=584%2C225&ssl=1" data-permalink="https://r.prevos.net/r-sweave-and-latex/openbook_1000x647/" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=584%2C225&amp;ssl=1" alt="Using R with Emacs and ESS, Org Mode and LaTeX" data-image-title="Body Image paper" sizes="(max-width: 584px) 100vw, 584px" width="450" data-comments-opened="1" class="wp-image-1022" data-recalc-dims="1"></a>
<p class="wp-caption-text">Body Image research paper, written with Org Mode, R and LaTeX.</p>
</div>
<p>The post <a rel="nofollow" href="https://r.prevos.net/using-r-with-emacs/" target="_blank">Using R with Emacs and ESS</a> appeared first on <a rel="nofollow" href="https://r.prevos.net/" target="_blank">The Devil is in the Data</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="https://r.prevos.net/using-r-with-emacs/"> The Devil is in the Data</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CCTeF07VD9Q" height="1"></a></div>