This is the development code of R.nvim which improves Neovim's support to edit R scripts.
If you use a plugin manager, follow its instructions on how to install plugins
from GitHub. Users of lazy.nvim who
opted for defaults.lazy=true have to configure R.nvim with lazy=false.
Examples of configuration for lazy.nvim (see also cmp-r):
Minimal configuration:
{
'jalvesaq/tmp-R-nvim',
lazy = false
},
More complex configuration:
{
'jalvesaq/tmp-R-nvim',
config = function ()
-- Create a table with the options to be passed to setup()
local opts = {
R_args = {'--quiet', '--no-save'},
hook = {
after_config = function ()
-- This function will be called at the FileType event
-- of files supported by R.nvim. This is an
-- opportunity to create mappings local to buffers.
if vim.o.syntax ~= "rbrowser" then
vim.api.nvim_buf_set_keymap(0, "n", "<Enter>", "<Plug>RDSendLine", {})
vim.api.nvim_buf_set_keymap(0, "v", "<Enter>", "<Plug>RSendSelection", {})
end
end
},
min_editor_width = 72,
rconsole_width = 78,
disable_cmds = {
"RClearConsole",
"RCustomStart",
"RSPlot",
"RSaveClose",
},
}
-- Check if the environment variable "R_AUTO_START" exists.
-- If using fish shell, you could put in your config.fish:
-- alias r "R_AUTO_START=true nvim"
if vim.env.R_AUTO_START == "true" then
opts.auto_start = 1
opts.objbr_auto_start = true
end
require("r").setup(opts)
end,
lazy = false
},The complete list of options is in the documentation.
Please read the plugin's documentation for instructions on usage.
During conversion of VimScript to Lua, we decide to end support for features
that were useful in the past but no longer sufficiently valuable to be worth
the effort of conversion. We removed support for Rrst (it seems that not
many people use it anymore), debugging code (a debug adapter would be better),
legacy omni-completion (auto completion with
nvim-cmp is better), and highlighting
functions from .GlobalEnv (difficult to make compatible with tree-sitter + LSP
highlighting).
We changed the key binding to insert the assignment operator (<-) from an
underscore (which was familiar to Emacs-ESS users) to Alt+- which is more
convenient (but does not work on Vim).
We replaced the options R_source and after_R_start with hook and we can
insert other hooks for Lua functions at other parts of the code under user
request.
We removed the "echo" parameters from the functions that send code to R
Console. Users can still set the value of source_args to define the
arguments that will be passed to base::source() and include the argument
echo=TRUE. Now, there is a new option to define how many lines can be sent
directly to R Console without saving the code in a temporary file to be
sourced (max_lines_to_paste).
We reduced the options on how to display R documentation to: "split",
"tab", "float" (not implemented yet), and "no".
The options openpdf and openhtml were renamed as open_pdf and
open_html, with a minor change in how they behave.
There are two new commands:
-
:RMapsDescdisplay the list of key bindings followed by short descriptions. -
:RConfigShowdisplay the list of configuration options and their current values.
There is one new command to send the above piped chain of commands. It's
default key binding is <LocalLeader>sc.
If you have colorout installed, and if you are not loading it in your
~/.Rprofile, it should be the development version. Reason: R.nvim calls the
function colorout::isColorOut() which actually enables the colorizing of
output in the released version of colorout. This bug was fixed in this
commit.
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 some packages were loaded. The messages are in magenta 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.frameis created, it is automatically displayed in the Object Browser. Messages about unrecognized types are in magenta 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.frameelements, the labels show up in the Object Browser. -
The next images show results of omni completion.
-
The last slide shows the output of
summary.
The diagram below shows how the communication between Neovim and R works.
The black arrow represents 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
chansend()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 the Windows operating system, Nvim-R can send a message to R (nvimcom) which forwards the command to R Console.
The R package nvimcom includes the application rnvimserver which is never used by R itself, but is run as a Neovim's job. That is, the communication between the rnvimserver and Neovim is through the rnvimserver standard input and output (green arrows). The rnvimserver application runs a TCP server. When nvimcom is loaded, it immediately starts a TCP client that connects to rnvimserver (red arrows).
Some commands that you trigger 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).
When new objects are created or new libraries are loaded, nvimcom sends 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 knitting an Rnoweb file and compiling the LaTeX result. Most of the information is transmitted through the TCP connection to the rnvimserver, but temporary files are used in a few cases.
-
languageserver: a language server for R.
-
colorout: a package to colorize R's output.