Skip to content
Sebastian edited this page Jun 25, 2024 · 8 revisions

Almost Zero configuration (not recommended)

We need cmp-r for auto-completion and tree-sitter parsers for "r", "markdown" and "rnoweb". Hence, if you didn't have a ~/.config/nvim directory, the commands below would be enough to install R.nvim using Neovim's built-in plugin manager:

mkdir -p ~/.config/nvim/pack/myplugins/start
cd ~/.config/nvim/pack/myplugins/start
git clone https://github.com/R-nvim/R.nvim
git clone https://github.com/R-nvim/cmp-r
git clone https://github.com/hrsh7th/nvim-cmp
git clone https://github.com/nvim-treesitter/nvim-treesitter

Now we need an init.lua to configure nvim-cmp:

~/.config/nvim/init.lua:

local cmp = require('cmp')
cmp.setup({
  sources = cmp.config.sources({{ name = 'cmp_r' }}),
  mapping = cmp.mapping.preset.insert({
    ['<CR>'] = cmp.mapping.confirm({ select = false }),
    ['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert, }),
    ['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert, }),
  })
})

And, finally, start nvim and install the missing parsers:

:TSInstall r
:TSInstall rnoweb

Minimal configuration

The minimal ~/.config/nvim directory suggested here contains just one file: init.lua. In this example:

  • The plugin manager is lazy.nvim.
  • Besides R.nvim, we install cmp-r and nvim-cmp (for auto-completion), and nvim-treesitter (required to send functions to R Console).
  • We set the mappings of nvim-cmp to use <Tab> for selecting menu items.

~/.config/nvim/init.lua:

-- Must be before creating other maps:
-- vim.g.mapleader = ' '
-- vim.g.maplocalleader = ' '


-- Set your global variables and options above this line --

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
  "R-nvim/R.nvim",
  "R-nvim/cmp-r",
  {
    "nvim-treesitter/nvim-treesitter",
    run = ":TSUpdate",
    config = function ()
      require("nvim-treesitter.configs").setup({
	sync_install = true,
	ensure_installed = {
	  "r",
	  "markdown",
	  "markdown_inline",
	  "rnoweb",
	},
      })
    end,
  },

  {
    "hrsh7th/nvim-cmp",
    config = function()
      local cmp = require("cmp")
      cmp.setup({
	sources = {{ name = "cmp_r" }},
	mapping = cmp.mapping.preset.insert({
	  ['<CR>'] = cmp.mapping.confirm({ select = false }),
	  -- During auto-completion, press <Tab> to select the next item.
	  ['<Tab>'] = cmp.mapping(function(fallback)
	    if cmp.visible() then
	      cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })
	    elseif has_words_before() then
	      cmp.complete()
	    else
	      fallback()
	    end
	  end, { 'i', 's' }),
	  ['<S-Tab>'] = cmp.mapping(function(fallback)
	    if cmp.visible() then
	      cmp.select_prev_item({ behavior = cmp.SelectBehavior.Insert })
	    else
	      fallback()
	    end
	  end, { 'i', 's' }),
	}),
      })
      require("cmp_r").setup({ })
    end,
  },
}, {})

The command :RMapsDesc will display the list of R.nvim key bindings.

Other examples

This resource shows another way to set up a complete R development environment for neovim including R.nvim as well as other useful plugins: https://www.lazyvim.org/extras/lang/r