Skip to content
Jakson Alves de Aquino edited this page Dec 26, 2022 · 6 revisions

The procedure described in this section was tested years ago and may no long work...

The easiest way to run R in a remote machine is to log into the remote machine through ssh, start Neovim, and run R in a Neovim's terminal (the default). You will only need both Vim (or Neovim) and R configured as usual in the remote machine.

However, if you need to start either Neovim or Vim on the local machine and run R in the remote machine, then, a lot of additional configuration is required to enable full communication between Vim and R because by default both Nvim-R and nvimcom only accept TCP connections from the local host, and, R saves temporary files in the /tmp directory of the machine where it is running. To make the communication between local Vim and remote R possible, each application has to know the IP address of the other, and some remote directories must be mounted locally. Here is an example of how to achieve this goal (tested on April 2017).

  1. Setup the remote machine to accept ssh login from the local machine without a password (search the Internet to discover how to do it).

  2. At the remote machine:

    • You have to edit your ~/.Rprofile to create the environment variable R_IP_ADDRESS. R will save this value in a file that Vim has to read to be able to send messages to R. If the remote machine is a Linux system, the following code might work:

      # Only create the environment variable R_IP_ADDRESS if NVIM_IP_ADDRESS
      # exists, that is, if R is being controlled remotely:
      if(interactive() && Sys.getenv("NVIM_IP_ADDRESS") != ""){
          Sys.setenv("R_IP_ADDRESS" = trimws(system("hostname -I", intern = TRUE)))
      }

      If the code above does not work, you have to find a way of discovering the IP address of the remote machine (perhaps parsing the output of ifconfig).

  3. At the local machine:

    • Make the directories ~/.remoteR/NvimR_cache and ~/.remoteR/R_library.

    • Create the shell script ~/bin/mountR with the following contents, and make it executable (of course, replace remotelogin, remotehost and the path to R library with valid values for your case):

      #!/bin/sh
      sshfs remotelogin@remotehost:/home/remotelogin/.cache/Nvim-R ~/.remoteR/NvimR_cache
      sshfs remotelogin@remotehost:/home/remotelogin/R/x86_64-pc-linux-gnu-library/3.3 ~/.remoteR/R_library
    • Create the shell script ~/bin/sshR with the following contents, and make it executable (replace remotelogin and remotehost with the real values):

      #!/bin/sh
      ssh -t remotelogin@remotehost "PATH=/home/remotelogin/bin:\$PATH \
      NVIMR_COMPLDIR=~/.cache/Nvim-R \
      NVIMR_TMPDIR=~/.cache/Nvim-R/tmp \
      NVIMR_ID=$NVIMR_ID \
      NVIMR_SECRET=$NVIMR_SECRET \
      R_DEFAULT_PACKAGES=$R_DEFAULT_PACKAGES \
      NVIM_IP_ADDRESS=$NVIM_IP_ADDRESS R $@"
    • Add the following lines to your vimrc (replace hostname -I with a command that works in your system):

      " Setup Vim to use the remote R only if the output of df includes
      " the string 'remoteR', that is, the remote file system is mounted:
      if system('df') =~ 'remoteR'
          let $NVIM_IP_ADDRESS = substitute(system("hostname -I"), " .*", "", "")
          let R_app = '/home/locallogin/bin/sshR'
          let R_cmd = '/home/locallogin/bin/sshR'
          let R_compldir = '/home/locallogin/.remoteR/NvimR_cache'
          let R_tmpdir = '/home/locallogin/.remoteR/NvimR_cache/tmp'
          let R_remote_tmpdir = '/home/remotelogin/.cache/NvimR_cache/tmp'
          let R_nvimcom_home = '/home/locallogin/.remoteR/library/nvimcom'
       endif
    • Mount the remote directories:

      ~/bin/mountR
    • Start Neovim (or Vim), and start R.