question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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.

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
pgrammecommented, Apr 12, 2022

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.

0reactions
Yunuuuucommented, Dec 9, 2022

Just came across this problem, I also used conda to manage R in a remote server. Since the server has network limitations and I have no root permission, I can only work R with a conda, and set some local CRAN or Bioconductor mirror to install packages. here is the detail usage list:

  • use vscode to connect the server
  • install R with conda in the user path
  • install package use R function install.package (since I don’t want to use conda to manage R packages)
  • use conda to manage the R package compiling dependencies
  • use radian to provide Rterm

here is my sessionInfo() output:

R version 4.2.1 (2022-06-23)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /home/user/anaconda3/envs/R-release/lib/libopenblasp-r0.3.21.so

locale:
 [1] LC_CTYPE=en_US.UTF-8      
 [2] LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8      
 [8] LC_NAME=C                 
 [9] LC_ADDRESS=C              
[10] LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8
[12] LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.2.1   cli_3.4.1       
[3] tools_4.2.1      progressr_0.11.0
[5] jsonlite_1.8.3   digest_0.6.30   
[7] rlang_1.0.6     

I install R by command conda create --name R-release -c conda-forge r-base git radian here is my setting:

  • vscode setting (don’t use “~”, it seems vscode won’t expand it, just use a absolute path)
    "r.alwaysUseActiveTerminal": true,
    "r.bracketedPaste": true,
    "r.lsp.debug": false,
    "r.rpath.linux": "/home/user/anaconda3/envs/R-release/bin/R",
    "r.rterm.linux": "/home/user/anaconda3/envs/R-release/bin/radian",
    "r.rterm.option": [
        "--r-binary=/home/user/anaconda3/envs/R-release/bin/R",
        "--local-history",
        "--no-save",
        "--no-restore"
    ]
  • .Rprofile setting
# local CRAN and Bioconductor mirror
local({
    # set a CRAN mirror
    r <- getOption("repos")
    r["CRAN"] <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"
    options(repos = r)
    options(BioC_mirror = "https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
    # since the network limitation of my remote server, I used a local `bioconductor_config.yaml`
    # downloaded from https://bioconductor.org/config.yaml
    options(BIOCONDUCTOR_CONFIG_FILE = "~/.bioconductor_config.yaml")
})
# this is important for the installation of package from R command
local({
    home_dir <- path.expand("~")
    conda_env_dir <- file.path(home_dir, "anaconda3/envs")
    set_envpath <- function(env_var, ...) {
        if (any(lengths(list(...)) > 1L)) {
            stop("Element of length one is expected", call. = FALSE)
        }
        add_path <- file.path(...)
        add_path <- path.expand(add_path)
        env_var_value <- Sys.getenv(env_var, unset = "")
        if (identical(env_var_value, "")) {
            path <- add_path
        } else {
            path <- paste0(add_path, switch(.Platform$OS.type,
                windows = ";",
                unix = ":"
            ), env_var_value)
        }
        expr <- quote(Sys.setenv())
        expr[[env_var]] <- path
        eval(expr)
    }

    # R-release environment path -----------------------------
    # conda create --name R-release -c conda-forge r-base git radian
    conda_renv <- file.path(conda_env_dir, "R-release")
    set_envpath("PKG_CONFIG_PATH", conda_renv, "lib", "pkgconfig")

    # pkg-specific PKG_CONFIG_PATH ---------------------------
    # these packages are not compitable with current conda R environment, install
    # this package in another conda environment and add the corresponding
    # PKG_CONFIG_PATH
    # gert environment variable
    # conda create -n libgit2 -c conda-forge libgit2
    libgit2 <- file.path(conda_env_dir, "libgit2")
    set_envpath("PKG_CONFIG_PATH", libgit2, "lib", "pkgconfig")
    set_envpath("LD_LIBRARY_PATH", libgit2, "lib")

    # textshaping environment variable
    # harfbuzz
    # conda create -n harfbuzz -c conda-forge harfbuzz
    harfbuzz <- file.path(conda_env_dir, "harfbuzz")
    set_envpath("PKG_CONFIG_PATH", harfbuzz, "lib", "pkgconfig")
    # fribidi
    # conda create -n fribidi -c conda-forge fribidi
    fribidi <- file.path(conda_env_dir, "fribidi")
    set_envpath("PKG_CONFIG_PATH", fribidi, "lib", "pkgconfig")
})
  • .Renviron setting
LANGUAGE=en
R_HISTSIZE=500
# for I have no root permission, I use a user R library path
R_LIBS_USER=~/Rlibrary/%v
# add your conda R environment path to the `PATH` variable
PATH=/home/user/anaconda3/envs/R-release/bin:${PATH}
  • last setting (optional), just for convenience of the usage of R in bash. .bashrc setting:
alias r="~/anaconda3/envs/R-release/bin/radian --r-binary ~/anaconda3/envs/R-release/bin/R"
alias R=~/anaconda3/envs/R-release/bin/R
alias Rscript=~/anaconda3/envs/R-release/bin/Rscript
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remote Development Tips and Tricks - Visual Studio Code
Visual Studio Code Remote Development troubleshooting tips and tricks for SSH, Containers, and the Windows Subsystem for Linux (WSL)
Read more >
Remote Development using SSH - Visual Studio Code
Connect to a remote host​​ In VS Code, select Remote-SSH: Connect to Host... from the Command Palette (F1, Ctrl+Shift+P) and use the same...
Read more >
Remote Development FAQ - Visual Studio Code
Remote Development FAQ. This article covers frequently asked questions for each of the Visual Studio Code Remote Development extensions. See the SSH ......
Read more >
Connect to remote Docker over SSH - Visual Studio Code
Connect via SSH to Docker engine running on a remote machine and use the remote machine as a development environment for Visual Studio...
Read more >
Visual Studio Code Workspace Trust security
When prompted by the Workspace Trust dialog, if you choose No, I don't trust the authors, VS Code will go into Restricted Mode...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found