Emacs ESS and LSP


This is a quick guide to setup Emacs with ESS and LSP.

Introduction

Emacs is great. I love it. And I also enjoy using R. Good thing I'm a not alone. Thank you maintainers of ESS - Emacs Speaks Statistics.

ESS will add some very handy capabilities to your Emacs so you can do some R coding in it.

However, one of the nicest additions, the Language Server Protocol - LSP, can be tricky to install and get it working - I'm assuming you're using Linux, please keep this information in mind.

Installation

Considering that you already have Emacs installed, you'll need to install ESS. How to install it can vary depending how you're using Emacs and your distro.

In my case, I use Spacemacs and installing ESS is very easy. Just add ESS to your .spacemacs layers (M-m f e d):

  ;; List of configuration layers to load.
   dotspacemacs-configuration-layers
   '(ess
     ;;your other layers
     )

Restart Emacs and start a R session (M-x R).

Now you'll need to install "lintr" and "languageserver" for R. To do so, in the R console type:

  install.packages("lintr")
  install.packages("languageserver")

This is important. The tow packages must install without any errors. If you get errors, read it carefully.

Depending on the distro you are using, it is possible that you may need to install packages related to C/C++ development (R packages are compiled from source during install and missing development tools may result in packages failing to install).

Here's an example:

  ------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libcurl was not found. Try installing:
 * deb: libcurl4-openssl-dev (Debian, Ubuntu, etc)
 * rpm: libcurl-devel (Fedora, CentOS, RHEL)
 * csw: libcurl_dev (Solaris)
If libcurl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘curl’
removing ‘/home/christian/R/x86_64-mageia-linux-gnu-library/4.0/curl’
 * installing *source* package ‘openssl’ ...
 * package ‘openssl’ successfully unpacked and MD5 sums checked
 ** using staged installation
 ** Using PKG_CFLAGS=
--------------------------- [ANTICONF] --------------------------------
Configuration failed because openssl was not found. Try installing:
 * deb: libssl-dev (Debian, Ubuntu, etc)
 * rpm: openssl-devel (Fedora, CentOS, RHEL)
 * csw: libssl_dev (Solaris)
 * brew: [email protected] (Mac OSX)
If openssl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a openssl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'

Even though I had the required packages to compile C/C++ I was missing curl-devel and openssl-devel. BTW, I use many different distros and I have different errors of missing dependencies. So, read the error message carefully to identify what is exactly that you are missing. Also, sometimes the name of the package that you need is not an exact match to the one in the error message. You'll have to do some digging in your distro's repo to find exactly what you need.

All in place, run the command to install the R packages again. The install must complete without any error messages.

Finally, you need to tell emacs to use lsp as a backend for your ESS mode. In Spacemacs you just need to add the line to your layers:

  ;; List of configuration layers to load.
   dotspacemacs-configuration-layers
   '(ess
    (ess :variables ess-r-backend 'lsp)
     ;;your other layers
    )

That's it.

Using

Just open a R file. It should automatically open in ESS mode with LSP enabled. If LSP fails to start, check if you are in ESS mode and make sure you'e followed all the steps to install it (remember, you must be able to install the required R packages without any errors!).

Depending on the version of Emacs you are using, the configuration may be different. Also, depending on the Linux distro you are using, system dependencies may also be different.

Happy R coding.