-
Notifications
You must be signed in to change notification settings - Fork 40
Configuration
R.nvim does not require any configuration to work, but it
requires tree-sitter configured to send code to R.
A completion plugin is required for autocompletion.
In this example, we use the built-in vim.pack module to install the plugins, and tree-sitter-manager to install tree-sitter parsers.
vim.pack.add({
"https://github.com/romus204/tree-sitter-manager.nvim",
"https://github.com/saghen/blink.lib",
"https://github.com/Saghen/blink.cmp",
"https://github.com/R-nvim/R.nvim"
})
require("tree-sitter-manager").setup({
-- Possibly embedded in Quarto, RMarkdown, Rnoweb, and RTypst
ensure_installed = { "yaml" },
-- Everything else
auto_install = true,
})
require("blink-cmp").setup({
sources = {
default = {
"lsp",
"path",
},
},
signature = { enabled = true },
})The vim.pack module is not available in Neovim 0.11.4, and we use lazy.nvim as plugin manager.
Neovim 0.11.4 requires the "master" branch of nvim-treesitter and the "v1" branch of blink.cmp.
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)
-- Setup lazy.nvim
require("lazy").setup({
spec = {
{
"nvim-treesitter/nvim-treesitter",
branch = "master",
config = function()
local langs = { "r", "markdown", "markdown_inline", "rnoweb", "typst", "yaml", "csv" }
vim.api.nvim_create_autocmd("FileType", {
pattern = langs,
callback = function() vim.treesitter.start() end,
})
end,
},
{
"saghen/blink.cmp",
branch = "v1",
opts = {
sources = {
default = {
"lsp",
"path",
},
},
},
signature = { enabled = true },
}
},
"R-nvim/R.nvim",
})After creating the above init.lua, start Neovim and run the command:
:TSInstall r markdown markdown_inline rnoweb typst yaml csv
With this shortcut, you can read the YAMLheader of the.Rmdfile (the saved version of the file) into theparams` variable.
{
"R-nvim/R.nvim",
keys = {
{
"<LocalLeader>pr",
"<cmd>lua require('r.send').cmd('params <- lapply(knitr::knit_params(readLines(\"' .. vim.fn.expand(\"%:p\") .. '\")), function(x) x$value); class(params) <- \"knit_param_list\"')<CR>",
desc = "read params from the YAML header",
},
}
}You can add this to your .Rprofile file to use the modern httpgd device as the default graphics device:
options(device=httpgd::hgd)This requires httpgd package to be installed.
You can also bind this to a shortcut to open the browser:
{
"<LocalLeader>gd",
"<cmd>lua require('r.send').cmd('tryCatch(httpgd::hgd_browse(),error=function(e) {httpgd::hgd();httpgd::hgd_browse()})')<CR>",
desc = "httpgd",
},Please note that httpgd is not currently hosted on CRAN due to an issue with some upstream dependencies. The package maintainer suggests that users install the package from GitHub.
remotes::install_github("nx10/httpgd")
# Alternatively, R-Universe still hosts the package
install.packages('httpgd', repos = c('https://remlapmot.r-universe.dev'))Terminalgraphics is an R package that defines a graphics device and functions for graphical output in terminal emulators that support graphical output, such as Kitty and WezTerm.
If you run R in an external terminal emulator or split pane, you can add this to your .Rprofile file to use the R package terminalgraphics as the default graphics device using these settings as recommended by the package maintainer:
options(device = terminalgraphics::tgp, term_col = TRUE)For more information about the terminalgraphics package and for a list of supported terminal emulators, please refer to the package repository.
Add this to your nvim-treesitter configuration to get webr and pyodide recognized as r and python respectively:
{
"nvim-treesitter/nvim-treesitter",
config = function ()
vim.treesitter.language.register("r", "webr")
vim.treesitter.language.register("python", "pyodide")
end,
}