Skip to content
Iakov Davydov edited this page Nov 20, 2024 · 15 revisions

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 with highlighting enabled (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",
          "yaml",
	},
	highlight = {
	  enable = true,
	},
      })
    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,
  },
}, {})

-- vim: sw=2 noexpandtab

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

Using params in R Markdown

With this shortcut you can read YAML header of the .Rmd file (the saved version of the file) into the params 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",
      },
    }
  }

Using httpgd as the default graphics device

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",
      },

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