vscode-R: I can't attach any R session to VS Code when using a remote server via SSH and it can't find the binary either.

I am using a conda virtual environment for a research project conducted at a remote server which I access via SSH.

I use the [Remote SSH] (https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh) extension to connect to the host and then I need to edit R files there. The code below shows that both R and radian are correctly set, I believe:

(r_conda) [rgr6291@klc0201 bin]$ conda env list
# conda environments:
#
fomc                     /home/rgr6291/.conda/envs/fomc
r_conda               *  /home/rgr6291/.conda/envs/r_conda
rfe_paper                /home/rgr6291/.conda/envs/rfe_paper
base                     /software/python-anaconda3/2019.10

(r_conda) [rgr6291@klc0201 bin]$ which R
~/.conda/envs/r_conda/bin/R
(r_conda) [rgr6291@klc0201 bin]$ which radian
~/.conda/envs/r_conda/bin/radian

In the settings for the R extension, I have set these binaries as the RPath and the Rterm:

{
    "r.rpath.linux": "~/.conda/envs/r_conda/bin/R",
    "r.rterm.linux": "~/.conda/envs/r_conda/bin/radian"
}

However, I get errors like this one when I try to attach an R session:

Cannot find R client at ~/.conda/envs/r_conda/bin/radian. Please check r.rterm setting.

And also it complains that it can’t find the jsonlite package although it has been installed:

Failed to get list of R functions. Make sure that jsonlite is installed and r.rpath.linux points to a valid R executable.

What am I missing here?

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 4
  • Comments: 21 (6 by maintainers)

Most upvoted comments

Hi !

My environment is the same as raulguarini (OP): VsCode + R extension + SSH remote shell (Linux) + R only available through a Conda environment on the remote. I haven’t tried to use radiant. After struggling a bit I just found how to use the Conda env properly, so I’m sharing it here.

In my case, the Conda environment also contains R packages (tidyverse etc) and an ODBC driver that I need to use from R. In order to use syntax highlighting from VsCode, I installed r-languageserver and r-jsonlite in my Conda environment.

Three options: the first one is a bit easier but not complete enough for me. I went for Option 3.

I hope this can help someone else! If so, maybe document it somewhere? Pierre

Option 1

In the settings for the R extension, set RPath and the Rterm: (NB: I avoided “~” in the paths, not sure if expansion would work or not)

{
    "r.rpath.linux": "/home/myself/conda/envs/my_conda_env/bin/R",
    "r.rterm.linux": "/home/myself/conda/envs/my_conda_env/bin/R"
}

Result: R is available, tidyverse also OK but not the ODBC driver is not accessible from R.

Option 2

As suggested in #623, you can try to set "r.alwaysUseActiveTerminal": true in the VsCode settings. Then manually start a terminal, Conda-activate and start R. After that you should be able to submit R commands (e.g. with Ctrl+Enter). I find it less comfortable and didn’t try it.

Option 3

Write a little shell script which first activates Conda and then starts R. I wrote it in /path/to/myproject/R-conda.sh and made it executable (chmod +x R-conda.sh)

#!/bin/bash
MY_CONDA_ENV=my_conda_env

# Optional and not related to Conda: cd to the script's dir
cd $(dirname $0)

# First we need to init conda bc ~/.bashrc is not executed when starting non-interactive shells. 
# Assuming PATH was already set by parent shell, and reusing script in profile.d/ if possible...
__conda_init_file=$(dirname $(which conda))/../etc/profile.d/conda.sh
if [ -f "$__conda_init_file" ]; then
    source "$__conda_init_file"
else
    eval "$(conda shell.bash hook 2> /dev/null)"
fi    

# Now we can activate
conda deactivate && conda activate $MY_CONDA_ENV

# And start R, passing through all command-line arguments
R "$@"

Then I pointed VsCode-R to this script in the settings:

{
    "r.rpath.linux": "/path/to/myproject/R-conda.sh",
    "r.rterm.linux": "/path/to/myproject/R-conda.sh"
}

Result: R is available, tidyverse and the ODBC driver are accessible from R.

Hi !

My environment is the same as raulguarini (OP): VsCode + R extension + SSH remote shell (Linux) + R only available through a Conda environment on the remote. I haven’t tried to use radiant. After struggling a bit I just found how to use the Conda env properly, so I’m sharing it here.

In my case, the Conda environment also contains R packages (tidyverse etc) and an ODBC driver that I need to use from R. In order to use syntax highlighting from VsCode, I installed r-languageserver and r-jsonlite in my Conda environment.

Three options: the first one is a bit easier but not complete enough for me. I went for Option 3.

I hope this can help someone else! If so, maybe document it somewhere? Pierre

Option 1

In the settings for the R extension, set RPath and the Rterm: (NB: I avoided “~” in the paths, not sure if expansion would work or not)

{
    "r.rpath.linux": "/home/myself/conda/envs/my_conda_env/bin/R",
    "r.rterm.linux": "/home/myself/conda/envs/my_conda_env/bin/R"
}

Result: R is available, tidyverse also OK but not the ODBC driver is not accessible from R.

Option 2

As suggested in #623, you can try to set "r.alwaysUseActiveTerminal": true in the VsCode settings. Then manually start a terminal, Conda-activate and start R. After that you should be able to submit R commands (e.g. with Ctrl+Enter). I find it less comfortable and didn’t try it.

Option 3

Write a little shell script which first activates Conda and then starts R. I wrote it in /path/to/myproject/R-conda.sh and made it executable (chmod +x R-conda.sh)

#!/bin/bash
MY_CONDA_ENV=my_conda_env

# Optional and not related to Conda: cd to the script's dir
cd $(dirname $0)

# First we need to init conda bc ~/.bashrc is not executed when starting non-interactive shells. 
# Assuming PATH was already set by parent shell, and reusing script in profile.d/ if possible...
__conda_init_file=$(dirname $(which conda))/../etc/profile.d/conda.sh
if [ -f "$__conda_init_file" ]; then
    source "$__conda_init_file"
else
    eval "$(conda shell.bash hook 2> /dev/null)"
fi    

# Now we can activate
conda deactivate && conda activate $MY_CONDA_ENV

# And start R, passing through all command-line arguments
R "$@"

Then I pointed VsCode-R to this script in the settings:

{
    "r.rpath.linux": "/path/to/myproject/R-conda.sh",
    "r.rterm.linux": "/path/to/myproject/R-conda.sh"
}

Result: R is available, tidyverse and the ODBC driver are accessible from R.

Option 2 worked for me after conda installing r-languageserver and r-jsonlite - thanks so much for this post!! I like option 2 for constructing new code, but option 3 with command line args is probably better for debugging.