Site icon R-bloggers

R with Vim on Mac OS X

[This article was first published on Computational Mathematics » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The built-in script editor for the Mac OS X R GUI actually isn’t bad. In fact it is much better than its Window’s counterpart. In particular, it has:

However, when coding in R and pretty much any other language, Vim has always been my goto text editor of choice. Once you get past the fairly steep learning curve, nothing comes close to it in terms of coding efficiency and navigation except perhaps Emacs, but let’s not go there.

Michael Bojanowski wrote a blog post on using R with Vim on an Ubuntu machine. One commenter asked about getting it to work on Mac OS X.

First of all, I highly recommend the MacVim port of Vim (not to be confused with macvim.org). You can get the latest version (7.3.53 at time of post) at github:

https://github.com/b4winckler/macvim/downloads

The Vim-R-plugin that Michal mentioned works on OS X with the Screen plugin as well. However, a simpler method is to use one of the following two OS X specific scripts that rely on AppleScript.

R.vim
R.vim uses AppleScript to send selected lines of code to an R buffer. Simply hit <F3> to run the selected lines in R.  If R is not open, it will automatically open a new session. Note that it opens R.app instead of R64.app so if you need to run the 64-bit version you’ll have to tinker with the script a bit.

R-MacOSX
R-MacOSX is also pretty straight forward, although I couldn’t get it working properly. It defaults to <Cmd-E> to run selected lines and <Shift-Cmd-E> to source the entire file.

In either case, key mappings are customizable but personally, I have no trouble with <F3> to execute code using R.vim.  Neither are as full-featured as Vim-R-plugin but I find them sufficient as all I really need is to send blocks of code to R.

Happy Vimming!

To leave a comment for the author, please follow the link and comment on their blog: Computational Mathematics » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.