sponsor Vim development Vim logo Vim Book Ad

R.vim : Send R code from a VIM buffer to R

 script karma  Rating 155/54, Downloaded by 5506  Comments, bugs, improvements  Vim wiki

created by
Johannes Ranke
script type
The plugin is not maintained any more. Please use the excellent vimscript #2628. It even lets you use the "r" key to send a visual selection to R (let vimrplugin_map_r=1) if you got used to this using my plugin. The following is only kept for archiving purposes.

Send R code from a VIM buffer to R on Unix type systems using funnel.pl from vimscript #221. Contains


Fernando Henrique Ferraz Pereira da Rosa is now a coauthor of this plugin (starting from version 0.03). He helped me fixing a bug and started the syntax file for mixed highlighting of Latex and R syntax (syntax/rnoweb.vim).

I set up a small project page for this plugin, featuring a screenshot:


You can browse the subversion repository where these files are at home: http://kriemhild.uft.uni-bremen.de/viewcvs/?root=vim

Alternatives for Mac Os X include vimscript #1741 and vimscript #2104.

Under Windows, I am now using a quite simple solution, consisting of the installation of the rcom package for R which requires statconnDCOM from http://rcom.univie.ac.at and ActiveState Perl in the PATH (watch out if you use R tools, because GNU perl does not have the DCOM interface, so ActiveState perl needs to be first in the path).
My ftplugin/r.vim under Windows:

perl << EOF
sub rsourceclipboard
    use Win32::OLE;
    my $R = Win32::OLE->GetActiveObject('RCOMServerLib.StatConnector')
          || Win32::OLE->new('RCOMServerLib.StatConnector');
    $R->Evaluate("source('clipboard', echo=TRUE)");

vnoremap <buffer> r "*y:perl rsourceclipboard<CR>

This means that the visual selection is pasted to the clipboard and the clipboard content is sourced by R when the key "r" is pressed in visual mode.

install details
For working with vim and R it is convenient to open a fresh xterm on a
fresh virtual desktop, covering the left half of the screen, and start vim,
check if the filetype is set correctly (:set ft), set it if necessary (e.g.
with :set ft=r) and press <F2>. This will open a second xterm listening to an R
interpreter.  You can then send visually highlighted code from vim to R with
the vmap "r".

Caution: If there are adjacent tabs in the source code (can be made visible
with :set list, then they show up as ^I^I), then your R console will list
the contents of your working directory in the middle of interpreting your
code and most likely get confused. I don't recommend using tabs anyway (use
the vim option expandtab, as set in r.vim). A workaround for existing code
is to execute it with the R command source().


As an alternative to this plugin on www.vim.org you can also install my Debian
package if you are using the Debian distribution:

$ su
$ echo "deb http://www.uft.uni-bremen.de/chemie/ranke/debs ./" >> /etc/apt/sources.list
$ apt-get update
$ apt-get install vim-r-plugin

Note that you have to activate addons like this plugin since vim version
7.1-022+1, July 2007. For system-wide activation, use

    $ vim-addons -w install r-plugin

(details in README.Debian). You can then ignore the rest of this file.


Making this plugin work requires the installation of the perl script,
ftplugin/funnel.pl. It just has to be in your $PATH and executable, so if you
are the only one working with the plugin on your machine, e.g.

mv ftplugin/funnel.pl ~/bin
chmod +x ~/bin/funnel.pl

(be sure ~/bin is in your $PATH variable) or if you have superuser privileges
you can put funnel.pl it into /usr/local/bin.

funnel.pl requires two perl libraries:

IO::Pty and Term::ReadLine

If you add the lines

au BufNewFile,BufRead *.R     setf r
au BufNewFile,BufRead *.R     set syntax=r

to your ~.vim/filetype.vim file (or create one with only these lines) then the
vimscript r.vim will be activated upon opening any file with a filename
matching *.R. The whole set of commands available at this point is elucidated
in ftplugin/r.vim.

I am also including my syntax files for R help files and R noweb files (Sweave files)
as well as a Makefile for processing R noweb files from within vim with :make. The
syntax files are part of vim starting from version 7.

I have one report of the funnel.pl not working on Mac OS X with the default perl on this
OS. If anybody gets this to work, please drop me a line.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
R_with_vim_0.5-80.tar.gz 0.5-80 2007-11-21 6.0 Johannes Ranke - Cleaned up ftplugin/*.vim files
- Added shiftwidth=2 and expandtab to ftplugin/rnoweb.vim
- Added documentation on using vim-addon-manager to activate the plugin under Debian based distributions
R_with_vim_0.5-62.tar.gz 0.5-62 2006-05-24 6.0 Johannes Ranke - Update of R help file syntax for new items in R 2.3.0
- Fixed tabbing in ftplugin/r.vim to suit emacs edited scripts.
- Added a note about the problem with adjacent tabs being pasted to the R console (see README.R_with_vim)
- Small fixes
R_with_vim_0.5-54.tar.gz 0.5-54 2006-04-18 6.0 Johannes Ranke - Up-to-date version of the R help and R noweb syntax files. Both of them are now part of vim 7.
- Added ftplugin/rnoweb.vim and Makefile.rnoweb for R noweb files.
R_with_vim_0.5-38.tar.gz 0.5-38 2006-02-28 6.0 Johannes Ranke - switched to funnel.pl from VIlisp 2.3
- carefully reviewed and extended the R help file syntax highlighting
R_with_vim_0.03-36.tar.gz 0.03-33 2005-08-09 6.0 Johannes Ranke Fixed the problem with the xterm not closing. Installation is also easier now because the bash script R-managed is not needed any more.
R_with_vim_0.03-29.tar.gz 0.03-29 2005-08-05 6.0 Johannes Ranke Fernando Henrique Ferraz Pereira da Rosa inspired me to improve the
plugin. Also added mixed syntax highlighting for Sweave files according
to the tip of his.
R_with_vim_0.01-8.tar.gz 0.01-8 2004-07-30 6.0 Johannes Ranke Small improvement of README file
R_with_vim_0.01-6.tar.gz 0.01 2004-07-28 6.0 Johannes Ranke Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.