Ramblings on social science, social networks, statistics, data analysis, computing, game theory and alike

R with Vim

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:

Use Vim-R-pluginfor 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:

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

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 FilesVimvimfiles’. So:

Install Vim-R-plugin normally.

Install Latex-suite normally.

In ~/.vim/ftplugin remove the symbolic link ‘rnoweb.vim’ and replace it with a normal text file with the following content:

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..

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.

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).

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.

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.

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

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.

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?