1

In OSX, how can I make a mapping in Vim to send code embedded in a non-R file to R for execution using the r-plugin?

Andreas
  • 209
  • 2
  • 8

2 Answers2

1

To activate the vim-r-plugin in any type of file, you may change temporarily the file type, because it's a filetype plugin. Assuming you are using OSX, you installed properly the vim-r-plugin, I was able to achieve this way from a markdown (.md) file:

  1. In Vim, set your filetype as R: :set ft=R
  2. Start R for this session: localleader + r + f
  3. Select your code and press space bar
  4. Optionally, revert to the original filetype :set ft=markdown

If you want the vim-r-plugin working permanently for a filetype, I suppose you will have to make some changes in the ~/.vim/ftdetect/r.vim. Then create an appropriate file in ~/.vim/ftplugin/. Maybe, use the ~/.vim/ftplugin/r.vim file as a model. I didn't try it.

jomuller
  • 1,032
  • 2
  • 10
  • 19
  • Thanks. The key for me was `:set ft=R`. A now have the following mapping to send R code that I have as within `comment` environments in a TeX file to R: `nmap rl :set ft=R\l:set ft=tex` – Andreas Jan 25 '15 at 13:03
  • Are you trying to mix *LaTeX* code with *R* code? If yes, you should try to use the `.Rnw` name extension and add this file type in `~/.vim/ftdetect/r.vim`. Then you should use *Sweave* to render it (search *Sweave* in *vim-r-plugin* helper page). This should behave as for `.Rmd` files. – jomuller Jan 25 '15 at 13:19
0

Assuming Windows, there is a file clip2r.js in the batchfiles distribution.

To install it, place it on your PATH and add the 2 lines in the comments at the top of the source to your _vimrc file. It does not require any plugins.

To run it start gvim, enter some lines of R code, select them with the mouse and press F3 to send them to R.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • And assuming MAC? See edit. This solution did not seem to work in IOSX. – Andreas Jan 19 '15 at 08:11
  • I wouldn't expect it to work on other operating systems although you might be able to fashion something yourself that does the same thing. F3 sends the selected text to the clipboard and then it posistions itself in the main R window and performs a paste. – G. Grothendieck Jan 19 '15 at 12:59