How to Turn Vim Into an IDE for R

Kade Killary
Dec 10, 2017 · 8 min read

Warning: No, this is not the R setup to use if you are a beginner. The RStudio IDE is amazing and should probably always be your default tool. However, if you happen to belong to the outcast realms of Vim / Emacs land, then this post might be for you. Also, I’m going to mention Vim and Neovim throughout the post, at this point they are largely one in the same. So, if you are tied to one or the other it shouldn’t matter.

Why Not Just Use RStudio?

A great question indeed. For me, the main reasons are speed and familiarity. Yes, I know RStudio has Vim keybindings, but it isn’t the real thing. At this point, I’m ruined by Vim. A lone madman vigorously hitting <esc> and <C-f> in Microsoft Word only to be disappointed.

However, all is not lost.

R In Vim

Your best option will be to utilize a separate :terminal buffer. The basic workflow goes as follows:

  • Write code in myFile.R
  • Visually select code
  • Paste code in :terminal buffer
  • Execute code
  • Rinse and repeat

This may not seem too bad, however it gets tedious fairly quickly. Plus, this approach leaves a lot to be desired. Mainly, viewing what’s defined, perusing the data, and some basic completion + linting.

Nvim-R To The Rescue

The first step to R enlightenment is…you guessed it, installing Nvim-R. I use Vim-Plug, so that’s what I show below. However, you can just as easily install it using whatever plugin manager you choose.

Plug 'jalvesaq/Nvim-R'

Now if you open an R file and hit \rf you’ll see a terminal buffer appear with an R console tied to your current session. To end it, hit \rq.

R file + R console

One important thing to note, the console is not tied to just the current buffer. This means that you can have multiple buffers all feeding into the same console. This can be good / bad depending on your personal preferences. I enjoy it, but it can definitely throw you for a loop if you’re careless. For a deeper dive on how R and Vim communicate in Nvim-R you can head here.

Secret Sauce

Sending Lines

  • Send :: Entire File \aa
  • Send :: Entire Block \bb
  • Send :: Entire Function \ff
  • Send :: Entire Selection \ss
  • Send :: Entire Line \l

As you can begin to see the forward slash \ is the leader for many operations. However, most of these, and the minor distinctions between them, are superfluous. You will likely be better served by remapping a few of them.

" in your .vimrc /init.vim“ remapping the basic :: send line
nmap , <Plug>RDSendLine
“ remapping selection :: send multiple lines
vmap , <Plug>RDSendSelection
“ remapping selection :: send multiple lines + echo lines
vmap ,e <Plug>RESendSelection

I chose to remap the basic send line + multiple lines to my comma key. This significantly cuts down on the number of keys I have to utilize. Furthermore, the ,e mapping allows me to check that the lines I sent were computed correctly. For the most part, these three mappings will allow you to do everything you need to. There are a few more worth mentioning though, and may add something to your workflow if remapped.

Object Browser

Objects can also be viewed by typing \rl, which will run the ls() function in your current console.

Documentation

Another worthwhile option is the Dash plugin. The easiest path to usage is as follows:

“ install plugin :: using vim-plug
Plug ‘rizzatti/dash.vim’
“ remap search key
nmap <silent> <leader>d <Plug>DashSearch<CR>

Now, when you are seeking more information on a piece of R code, or any other language, all you have to do is place your key over the word and hit <leader>d. The Dash app will then pop up with the relevant information. You can also search Google and Stack Overflow from within it. It’s a great tool, especially for Vimmers who utilize Vim for a variety of languages.

Viewing Data

Nvim-R allows you to view a data frame by using the \rv command. This will either show the data frame using X Quartz, on Mac, or the CSV plugin for Vim, if you have it installed.

The CSV plugin comes with a whole host of additional features for manipulating data, but that is outside of the scope of this article. By and large, my suggestion would be to use Excel. While overly beloved by many, it does serve as a good data viewer.

Other Tips & Tricks

Inline Code Output

Functions

  • summary() :: \rs
  • plot() :: \rg
  • args() :: \ra
  • setwd() :: \rd
  • print() :: \rp
  • names() :: \rn

Arrows

Completion

Nvim-R Completion

Ncm-R

Ncm-R provides rich completions for all of the following:

  • Objects from the global R environment
  • Functions from all loaded packages
  • Packages inside library() and require()
  • Datasets inside data()
  • Arguments inside functions
  • Variables inside pipes %>% and ggplots +

For a basic set up add the code below to your Vim configuration file.

Plug ‘roxma/nvim-completion-manager’
Plug ‘gaalcaras/ncm-R’
“ Optional: for snippet support
Plug ‘sirver/UltiSnips’

R Language Server

Linting

Plug ‘w0rp/ale’

Now all you need to do is install lintr. This can be done by using install.packages(‘lintr’).

Now, the next time you open up an .R file you should be good to go.

Conclusion

For more about Vim head to my blog!


Bonus Section

“ settings :: Nvim-R plugin“ R output is highlighted with current colorscheme
let g:rout_follow_colorscheme = 1
“ R commands in R output are highlighted
let g:Rout_more_colors = 1

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Kade Killary

Written by

https://kadekillary.work

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade