This is the development code of Nvim-R which improves Vim's support to edit R code. It started as a copy of the Vim-R-plugin adapted to Neovim, but now also supports Vim.
The R package nvimcom is included in the source code and is automatically
installed and updated whenever necessary. The Nvim-R plugin sets the
environment variable R_DEFAULT_PACKAGES
, including nvimcom
in the list of
packages to be loaded on R startup.
If you use a plugin manager, such as vim-plug, Vundle or Pathogen, follow its instructions on how to install plugins from github.
To install a stable version of the plugin, download the Vimball file
Nvim-R.vmb
from
Nvim-R/releases,
open it and do:
:so %
Then, press the space bar a few time to ensure the installation of all files.
The Nvim-R source code includes the R package nvimcom, which has C code that
needs compilation. Nvim-R will build and install nvimcom automatically, and it
will also update it automatically whenever necessary. A C compiler (e.g. gcc
or clang
) is necessary. On Windows, you have to install
Rtools.
Please, read the plugin's documentation for instructions on usage.
Below is a sample init.vim
:
syntax on
filetype plugin indent on
"------------------------------------
" Behavior
"------------------------------------
let maplocalleader = ","
let mapleader = ";"
"------------------------------------
" Appearance
"------------------------------------
" www.vim.org/scripts/script.php?script_id=3292
colorscheme southernlights
"------------------------------------
" Search
"------------------------------------
set infercase
set hlsearch
set incsearch
"------------------------------------
" Nvim-R
"------------------------------------
if has("gui_running")
inoremap <C-Space> <C-x><C-o>
else
inoremap <Nul> <C-x><C-o>
endif
vmap <Space> <Plug>RDSendSelection
nmap <Space> <Plug>RDSendLine
Please, read the file doc/Nvim-R.txt for usage details.
The animated GIF below shows R running in a Neovim terminal buffer. We can note:
-
The editor has some code to load Afrobarometer data on Mozambique, R is running below the editor and the Object Browser is on the right side. On the R Console, we can see messages inform the packages setwidth and nvimcom were loaded. The messages are in blue because they were colorized by the package colorout.
-
When the command
library("foreign")
is sent to R, the string read.spss turns blue because it is immediately recognized as a loaded function (the Vim color scheme used is southernlights). -
When Mozambique's data.frame is created, it is automatically displayed in the Object Browser. Messages about unrecognized types are in blue because they were sent to stderr, and the line Warning messages is in red because colorout recognized it as a warning.
-
When the "label" attributes are applied to the data.frame elements, the labels show up in the Object Browser.
-
The last slide shows the output of
summary
. It also features omni completion in action: we can see the elements of m that start with "D".
In addition to sending lines of code to R Console, Nvim-R and R communicate with each other through TCP connections. The R package nvimcom runs a TCP server that receives messages from either Vim/Neovim, and it also sends messages through a TCP connection to Vim/Neovim. Moreover, nvimcom includes the application nclientserver which is never used by R itself, but is run by Vim/Neovim, providing both a TCP client and a TCP server. The Diagram below shows the three paths of communication between Vim/Neovim and R:
-
The black path is followed by all commands that you trigger in the editor and that you can see being pasted into R Console. There are three different ways of sending the commands to R Console:
-
When running R in a Neovim built-in terminal, the function
jobsend()
is used to send code to R Console. -
When running R in an external terminal emulator, Tmux is used to send commands to R Console.
-
On Windows operating system, the nclientserver.exe application includes Windows API calls to copy the text into the clipboard and then paste it into the R Console. There is also a dll that does the same. NeovimQt uses the application and GVim uses the dll.
-
-
The blue path is followed by the few commands that you trigger, but that are not pasted into R Console and do not output anything in R Console; their results are seen in the editor itself. These are the commands to do omnicompletion (of names of objects and function arguments), start and manipulate the Object Browser (
\ro
,\r=
and\r-
), call R help (\rh
or:Rhelp
), insert the output of an R command (:Rinsert
) and format selected text (:Rformat
). -
The red path is followed by R messages that tell the editor to update the Object Browser, update the syntax highlight to include newly loaded libraries and open the PDF output after weaving an Rnoweb file and compiling the LaTeX result.