[update]How to turn vim into a perfect, better R IDE and Rmarkdown writer than Rstudio (part I)

Xiaoou&NLP
5 min readJun 30, 2020

--

I saw an article here (https://medium.freecodecamp.org/turning-vim-into-an-r-ide-cd9602e8c217, How to Turn Vim Into an IDE for R) giving a brief tutorial on R and vim integration. It’s very nice but some details are missing and some information outdated.

Fork me on https://github.com/xiaoouwang/VIM-R-IDE-Vim_Config for the complete vimrc and make suggestions by issues and pull request.

This article gives a configuration to turn vim into a full-fledged R IDE.

AND guess what, it’s even better than Rstudio, check the screenshots below !

For a complete use of crazy good Nvim-R plugin 😈, check the doc here : https://github.com/jalvesaq/Nvim-R/blob/master/doc/Nvim-R.txt

Following functions are supported

1: snippets

2: copy image directly into r markdown using leader + p (see the [image] tag in rmarkdown file at the left)

3: run line, selection, chunk (leader + ss in normal mode, ss in visual mode, sc in normal mode)

Below is send chunk in rmarkdown

4: view objects, dataframes and plot and summary etc (leader + ro,rv,rg)…

  • objects at the center window
  • view dataframe by using csv.vim (powerful plugin too)

5: send output in comments and delete with undo (vim) (leader + st), very useful for have a quick glance of dataframe

👽6: fullscreen editing and writing experience with goyo.vim (ctrl +a)

7: citation using citation.vim

Vim part

Use neovim with python 3 support (by default)

Plugins (using vim-plug)

Plug 'jalvesaq/Nvim-R' " swissknife for vim and R communicationPlug 'Shougo/unite.vim' " for citation using citation.vimPlug 'jalvesaq/zotcite'   " for citation integration with zotero used in rmarkdownPlug 'rafaqz/citation.vim' " for citation used anywhere in md or rmd files" snippet framework beginningPlug 'ncm2/ncm2'  " snippet enginePlug 'roxma/nvi-yarp' " dependencyPlug 'gaalcaras/ncm-R' " snippetsPlug 'ncm2/ncm2-ultisnips' " ncm and ultisnips integrationPlug 'SirVer/ultisnips'  " snippet engine" snippets framework endPlug 'chrisbra/csv.vim' "for viewing data directly in vim R (Nvim-R)Plug 'junegunn/goyo.vim' "for nice zoom effet when editing, see screenshot belowPlug 'ferrine/md-img-paste.vim' "paste directly image in system clipboard to rmarkdown by putting images in an /img folder (created automatically

Configuration

Filetype

filetype plugin indent on" set rmarkdown file type for safetyau BufNewFile,BufRead *.Rmd set filetype=rmd

Ability to paste directly image in system clipboard to rmarkdown by putting images in an /img folder (created automatically)

" here i’m using leader+p to paste image to markdown and rmarkdownautocmd FileType markdown nmap p :call mdip#MarkdownClipboardImage()autocmd FileType rmd nmap p :call mdip#MarkdownClipboardImage()

Autocompletion

This is the tricky part, I’ll explain step by step

" First use tab and shift tab to browse the popup menu and use enter to expand:inoremap ncm2_ultisnips#expand_or("<CR>”, 'n')inoremap pumvisible() ? "<C-n>" : "<Tab>"inoremap pumvisible() ? "<C-p>" : "<S-Tab>"" However the previous lines alone won’t work, we must disable the UltiSnips Expand Trigger, I set it to ctrl-0let g:UltiSnipsExpandTrigger="<c-0>"

Tricks

let maplocalleader = ","" the default is , you can also set it to <\space> if you don’t like my setting" make R starts automatically when .R or .Rmd file open and only starts one timeautocmd FileType r if string(g:SendCmdToR) == “function(‘SendCmdToR_fake’)” | call StartR(“R”) | endifautocmd FileType rmd if string(g:SendCmdToR) == “function(‘SendCmdToR_fake’)” | call StartR(“R”) | endif" make R vertical split at startlet R_rconsole_width = 57let R_min_editor_width = 18" some nice keybindding, D = cursor down one line when finished the code
" localleader+rv = view data, +rg = plot(graphic), +rs = summary, all without sending lines to R buffer, very useful
" Other useful features like Rformat and R RBuildTags aren’t covered here, see Nvim-R for more info." useful when in Rmarkdown, send chunknmap sc RDSendChunk" directly send line to R buffer when nothing selectednmap ss RDSendLine" st = send test, this function shows the output in comment, since it’s in vim we can simply press u to make the output disappearnmap st RDSendLineAndInsertOutput" send selection in visual modevmap ss REDSendSelection" rq would be mapped to RClose so we replace RClearConsole by some random stringsvmap test RClearConsole nmap test RClearConsole "idemnmap rr RStart "rr is easier than rfvmap rr RStart "idemnmap rq RClose "rq = rquit, easier to remembervmap rq RClose "idem" map ctrl a (all screen) to goyo to have a fullscreen R editing and Rmarkdown writing experience

Citation

TO BE CONTINUED in PART II, follow me !

--

--

Xiaoou&NLP