Making R and Python available by default
There are certain tools that we expect to use often but that are only available to load via modules. For instance, we are likely to frequently make use of R, python, a compiler (GCC), or an editor (Neovim).
When we connect to the computing cluster, we open a shell, i.e. the
command-line interface there. The default for this is bash
,
which is usually available on all Unix-like systems. (However, it is no
longer default on macOS in favor of zsh
.)
Every time we start bash
on the login node, it runs its
configuration file, ~/.bashrc
. To automatically load our
standard tool set when we connect, we can add the
module load
commands to this file:
# User specific aliases and functions
if ! command -v R >/dev/null 2>&1; then
module load GCC/11.2.0 OpenMPI/4.1.1 R/4.1.2
module load GCCcore/11.2.0 Python/3.9.6 CMake/3.21.1
module load Neovim/0.6.1
fi
After we edited this file, we can either close the connection and
reconnect, or type source ~/.bashrc
to reload it. Note,
however, that loading these modules will result in a bit of a slowdown
to establish the connection to the login node. This may not be what you
want for normal use, but it will simplify working on the rest of the
course materials.
We can also change some other things, like adding colors to the prompt and have it display the current directory instead of just the folder name:
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h\[\e[1;31m\](sulis)\[\e[0m\]:\[\033[33;1m\]\w\[\033[m\]\$ "
And add some useful aliases to make listing files and navigating between different directories a bit easier:
alias l='ls -lh'
alias ll='ls -lah'
alias .='pwd'
alias cd..='cd ..'
alias ..='cd ..'
alias ...=' cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
Editing R scripts using Neovim
Configuration
For this, we edit the file ~/.config/nvim/init.vim
(create the directory first using mkdir -p
) to include:
set expandtab " tab as spaces (optional)
set shiftwidth=4 " shift by 4 spaces
set tabstop=4 " tab by 4 spaces
set smarttab " tab to same positions
"set autoindent " automatic indentation (good for editing, less for pasting)
set mouse=a " be able to use mouse clicks
set nobackup " use an undo file instead of backup files
set undofile
set undodir=~/.config/nvim/undo
tnoremap <Esc> <C-\><C-n> " fix Esc in terminal
" Uncomment this when adding the plugins\
# Note: this has to be listed before using the plugins (below)
"call plug#begin()
" Plug 'sheerun/vim-wombat-scheme'
" Plug 'jalvesaq/Nvim-R'
" Plug 'ycm-core/YouCompleteMe', { 'do': './install.py' }
" Plug 'ntpeters/vim-better-whitespace'
"call plug#end()
"colorscheme wombat " uncomment this after installing the plugin
" uncomment after installing Nvim-R, use <Space> to send commands to R
"let g:R_assign = 0 " do (not) map _ to <- for assignment
"vmap <Space> <Plug>RDSendSelection
"nmap <Space> <Plug>RDSendLine
" ^^^^^^ make sure there are are no extra spaces at the end of those lines
Plugins
(Neo)vim and emacs are powerful editors that have many useful functions not only for text editing, but also interactive development in R and other languages.
The functionality for this is provided by plugins. For emacs this is
mainly ESS (emacs speaks statistics), and for nvim we use the plugin
manager plug
, that we can install using the following lines
of code:
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
This will copy the file plug.vim
from Github into your
~/.local/share/nvim/site/autoload directory.
Note: before executing bits of code from the internet, make sure they are from trustworthy sources. In this case, it comes from the nvim-plug Github repository, which is the official source.
We will add the following plugins:
- Nvim-R
- YouCompleteMe
- Wombat color scheme
- Whitespace highlighter
For this, we uncomment in the file
~/.config/nvim/init.vim
:
call plug#begin()
Plug 'sheerun/vim-wombat-scheme'
Plug 'jalvesaq/Nvim-R'
Plug 'ycm-core/YouCompleteMe', { 'do': './install.py' }
Plug 'ntpeters/vim-better-whitespace'
call plug#end()
Note that we need Python and pynvim
for this to
work:
module load GCCcore/11.2.0 Python/3.9.6 CMake/3.21.1
pip install --user pynvim
We can then install the plugins in Neovim using Esc and:
:PlugInstall
Using R within nvim
Note: To avoid load on the login node, start the
nvim
in an interactive job if you plan on running
computations in R.
We can uncomment the lines previously commented in
~/.config/nvim/init.vim
. Now, using the Nvim-R
plugin, we can access R directly from nvim. For this, we can open any
.r
file, and then type
\rf.
We can now edit the file in the editor as we did before, but in addition send lines to R via Space, or is the terminal directly by selecting it with the mouse (or Ctrl+w followed by arrow keys).
Persistent sessions with tmux
So far, we have created a new terminal session each time we connected
to the login node. There are cases where we may instead want to continue
to work where we left off previously. There are tools that create a
“persistent” session on the login node that we can disconnect from and
later connect to without losing our work session. A well-known tool for
this is screen
, but more recently there is
tmux
. tmux
is installed on the login node, so
we can use it out of the box by typing:
tmux new
On a first view, not much has changed. In addition to our normal
terminal prompt, there is now a status line at the bottom of the screen.
We can now perform some operations, like typing ls
and
pwd
. When we want to disconnect, we can use the keys
Ctrl+b, d or type:
tmux detach
This will close the tmux
session. We can now disconnect
from the login node, later reconnect, and re-attach to the same session
by typing:
tmux attach -t 0
0
in this case the (automatic) name of the session we
created previously. In addition, we can also split our terminal in 2
vertical “panes” using Ctrl+b, % or
create a new “window” using Ctrl+b, c.
The panes, windows, and open applications (e.g. a text editor) will be
waiting for us each time we detach and re-attach to the session.
Ctrl+b is the common prefix, to be typed before
any other tmux command.
A ssht
alias to automatically connect to tmux
We can define the following function to automatically connect to (or
create) a tmux
session when we type ssht sulis
instead of ssh sulis
. If we are using the bash
shell locally as well, we can add these lines to our local
~/.bashrc
:
ssht() {
ssh -t "$1" 'tmux -2 -u attach || tmux -2 -u new'
}
Making tmux
easier to use
There are a lot of different commands to remember, especially when
first starting to use tmux
. We can add some configuration
options in ~/.tmux.conf
to make using it a bit more
intuitive:
## enable Ctrl+a as prefix for people used to "screen"
#set -g prefix C-a
#bind C-a send-prefix
#unbind C-b
# enable mouse clicks to select windows and panes
set -g mouse on
# easy-to-remember split pane commands
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# swap current pane with the next/previous one
bind > swap-pane -D
bind < swap-pane -U
After editing this config file, reload it using, from the Sulis shell:
tmux source-file ~/.tmux.conf