R programming in Emacs

If you want to do statistical calculations with your computer, you probably want to use some sort of package to help you. When I was in school, we used Minitab (a "mini" version of OMNITAB— now there's a great name!), but I guess most statisticians used either S or SAS at the time. Later, R came along as an alternative to S.

I may want to use extendr to access a Rust library from R, so I thought I would start by installing R on my Debian desktop.

sudo apt install r-cran-littler

"Little-R" has R 4.2 as a dependency on Debian bookworm, which is the minimum version required for extendr (Debian trixie will be released soon, which includes R 4.5).

$ R

R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> print("Hello, World!")
[1] "Hello, World!"

The first thing I always need when trying a new programming language is an Emacs mode for it. Turns out there isn't just something called r-mode, rather it's part of a larger thing called Emacs Speaks Statistics (ESS). To configure it with use-package, I ended up with a require inside of a use-package. That was a first!

(use-package ess
  :ensure t
  :init (require 'ess-r-mode))

Coincidentally, it was Emacs Berlin day and they happened to be talking about use-package and getting it to only load things as needed. I mentioned my require inside of use-package and when I showed it to them, they thought it would load unconditionally. Someone suggested expanding the macro (M-x pp-macroexpand-last-sexp RET), but it was very noisy.

Emacs window with pp-macroexpand-last-sexp applied to a use-package stanza

That is, use-package is itself a macro, so we've expanded it as well as the stuff we're interested in. But we're not the first ones with this problem because then someone suggested setting the use-package-expand-minimally variable.

(setq use-package-expand-minimally t)

When we did that, expanding the macro was much more sensible.

Emacs window with pp-macroexpand-last-sexp applied to same stanza with use-package-expand-minimally set

With a bit of fiddling, we came up with this

(use-package ess
  :ensure t
  :mode ("\\.R\\'" . ess-r-mode))

which, expanded to

Emacs window with pp-macroexpand-last-sexp applied to a modified use-package stanza

which we all agreed looked a lot more like it was autoloading on demand rather than loading unconditionally.

Incidentally, the "little r" mentioned above, is a command we can use in a shebang line for R scripts.

Screenshot of Emacs in ess-r-mode

Big R is an interactive program, as above.

Now I think I've got R installed and working in Emacs, so I'm ready to try to figure out extendr. Wish me luck!