Skip to content

R with Vim

December 2, 2010
tags: ,

For all those who think that Vim is The Editor for text files, and simultaneously think that R is The EnvironmentForStatisticalAnalysisAndGraphics.

After trying out various options for intergrating Vim with R I settled on the following configuration:

  1. Use Vim-R-plugin for editing R code files, R documentation files (*.Rd) as well as the Sweave files. Apart from syntax highlighting the plugin allows to open an R console in a separate window and operate it with keyboard shortcuts from Vim (no need for frequent alt-tabbing etc.). Among other things you can:
    • Execute individual code lines, visually selected portions, or whole R code files in the R console.
    • Putting a cursor on a function name in the code file and: display its R help page, or display function arguments (through args()).
    • Put a cursor on any R object in the code file and perform frequently used functions: str, summary, plot, print, names…
    • List the content of the R Workspace
    • Clean the R Workspace
  2. I use Sweave quite extensively. For Sweave files the Vim-R-plugin provides the same keyboard mappings as for the R code files as well as nicely highlights both the LaTeX code and the R code in the code chunks. As my Sweave files have mostly LaTeX code with rather short R code snippets I would like to take advantage of another Vim plugin: the LaTeX-Suite. By default Vim will not load the Latex-suite for Sweave files, which is a HUGE disadvantage.

Vim and R using Vim-R-Plugin in action

Here is a way how to use both plugins simultaneously for Sweave files. The instruction applies to Ubuntu (so probably any Linux-like system). On Windows the ‘~/.vim’ directory corresponds to the ‘vimfiles’ directory, which most likely is something like ‘c:\Program Files\Vim\vimfiles’. So:

  1. Install Vim-R-plugin normally.
  2. Install Latex-suite normally.
  3. In ‘~/.vim/ftplugin’ remove the symbolic link ‘rnoweb.vim’ and replace it with a normal text file with the following content:
runtime! ftplugin/r.vim
runtime! ftplugin/tex_latexSuite.vim

This will essentially load both plugins one after another. QED.

Edit

See here how to set it up on Mac

22 Comments
  1. December 3, 2010 03:14

    I couldn’t get it to work on Mac OS X. Do you know if anyone’s had any luck with it on non-Linux platforms?

    • December 3, 2010 23:49

      I stopped using Windows around two years ago, but I think I figured it out how to run it there. I can’t recall whether it was via Python or RCOM though… I have no idea whether it works Mac and how to run it there. The plugin’s page on vim.org clearly states that it should run on Mac too. Perhaps you could poke the guys here http://vim.wikia.com/wiki/Script:List_of_R_Project_scripts for help..

    • John permalink
      March 15, 2011 01:08

      This worked fine for me on OSX, but I think it you need to have xterm installed, because it boots up R in a screen session on xterm. If you can find the part of the script that boots it up you might be able to modify the plugin to use the regular OS-X terminal instead.

  2. January 11, 2011 08:06

    It works fine for me on Windows. As per the installation notes for Windows, I had to install Python. And I also had to install the conque shell plugin to get the R console as a buffer.

  3. Dan permalink
    January 15, 2011 15:56

    I find that indenting does not handle Sweave commands (<>= etc). What it does is to use the same indenting as in the line above. That is no good, if the above line is indented, as for example would be the case for an \item. The problem is that spaces at the start of Sweave commands causes “R CMD Sweave” ignores the commands. (Mac osx; mvim 7.3).

    • January 16, 2011 00:18

      Hmm…strange. vim-r-plugin on my ubuntu when I insert <>= properly puts it in the beginning of the new line irrespective of the current LaTeX indentation.

      • Dan permalink
        January 16, 2011 02:01

        In case it’s relevant to anyone on the thread at the moment (or who comes here later), I’m using “vim-latex-1.8.23-20101027-r1112“ and “vim-r-plugin-101217“ in my tests that reveal the problem with indenting “<>=“ chunks.

      • January 17, 2011 09:37

        Even stranger… as I have vim-r-plugin 101121 (so earlier than yours). I’m not sure where to check the latex-suite version though.

      • Dan permalink
        January 17, 2011 12:39

        The author of vim-r sent me a test version, and it works on my test files without any issues. Software development being a pains-taking process, I imagine it may be a while before the test version is ready for release on vim.org.

  4. mhnatiuk permalink
    July 1, 2011 09:48

    totally off the topic: I love the name of a folder on the first screen

  5. ejjunju permalink
    August 23, 2011 07:39

    The instructions for installation on windows are not clear to me!!

  6. ejjunju permalink
    August 23, 2011 07:43

    Foe instance when you say install install Vim-R-plugin normally, what does this mean? also when you say “remove the symbolic link ‘rnoweb.vim’ and replace it with a normal text file with the following content”, does this apply to windows? what name should i give the text file? By the way my folder doent even include a symbolic link!! it is actually empty.

    I am new to vim and only installed it because i thought that i could use it with Sweave and R. I am looking for nice detailed instructions for windows. also, when you say to replace text

    • August 23, 2011 08:57

      Did you look at vim-r-plugin page on vim.org (http://www.vim.org/scripts/script.php?script_id=2628)? Windows instructions are there. I think the recent versions of the package do not create any symbolic links, so you should be able to skip that step. All the other points I made apply to both Windows and Unix, and, probably, Mac.

      I hope that helps.

  7. okugami permalink
    September 18, 2011 11:35

    Hi, folks,

    Following are the most useful vim plugin for R user,
    It’s worth looking: Any way, Google it to find more information:

    1) To improve finding functions or keywords in multiple files,
    R plugin name: EasyGrep

    2) Improved version of working with multiple R script files, I found to be ctag to be too painful. This plugin works like IDE, allows to book map of files or multiple buffers

    R plugin name: NERDTree

    3) Learn and watch tmux demo on youtube
    It tells you how to split windows virtical, horizontal etc. It’s worth learning of tmux with r-plugin.

    okugami

    • September 19, 2011 09:19

      Hi. Thanks for the plugin suggestions. EasyGrep looks especially promising for my needs.

  8. March 1, 2012 00:04

    Thanks for this, having latex-suite and vim-r-plugin play nicely together has been fantastic.

    Question: I see you’re running R and vim in seperate terminal sessions — on my Ubuntu box I can get vim-R-plugin to display a split screen (with screen plugin) but I prefer your setup. Gvim will launch a new terminal running R when I hit \rf, but running vim from the terminal does not. How did you set that up?

    • March 1, 2012 11:19

      Thanks. Yes, I also find separate terminals much more convenient. I have the following lines in my .vimrc:

      au FileType r let vimrplugin_nosingler = 0
      au FileType r let vimrplugin_vimpager = "no"
      au FileType r let vimrplugin_by_vim_instance = 0
      au FileType r let vimrplugin_screenplugin = 0

      They set vimrplugin_* variables when you open an R file. I do not remember right now what each of the variables mean, but I’m sure you can look it up in the documentation.

    • March 1, 2012 13:12

      Oh, and the crucial one:

      let vimrplugin_term_cmd = “gnome-terminal –title R -e”

      that will make \rf open a new gnome terminal….

      I think that’s it. Let me know if it works.

Trackbacks

  1. R with Vim on Mac OS X « Computational Mathematics
  2. R Studio « Brokering the Closure

Comments are closed.