REditorSupport / vscode-R

R Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=REditorSupport.r
MIT License
1.08k stars 128 forks source link

Setup with conda installed R: attaching active Terminal fails #1487

Open skwde opened 9 months ago

skwde commented 9 months ago

Describe the bug conda installed packages are not found; not possible to attach active terminal session.

To Reproduce

Use following environment file to install R

---
name: r
channels:
  - conda-forge
dependencies:
  - r-base
  - r-renv
  - r-languageserver
  - r-httpgd
  - radian

and run

conda create -f environment.yml

to install the environment.

Ensure the settings as mentioned below.

Next clikc on R: (not attached) in the status bar in VSCode and get the error

The terminal process "<path to base>/.conda/envs/r/bin/radian '--no-save', '--no-restore'" terminated with exit code: 1.

Can you fix this issue by yourself? (We appreciate the help)

No

(If applicable) Please attach setting.json

"r.rpath.linux": "<path to base>/.conda/envs/r/bin/R",
"r.rterm.linux": "<path to base>/.conda/envs/r/bin/radian",

"r.libPaths": ["<path to base>/.conda/envs/r/lib/R/library"],

Expected behavior Terminal should attach

Screenshots If applicable, add screenshots to help explain your problem. You can show the keyboard contents by pressing F1 and Developer: toggle screencast mode

Environment (please complete the following information):

Additional context I already tried what is suggested here https://github.com/REditorSupport/vscode-R/wiki/R-Session-watcher#advanced-usage-for-self-managed-r-sessions as mentioned in this issue

EDIT:

There are also following related issues

rfhb commented 8 months ago

The system call to radian wrongly includes a comma after the first parameter:

"<omitted>/bin/radian '--no-save', '--no-restore'"

This seems unrelated to using conda or not, in my case without conda. I did not readily find the reason for why the system call is composed like this.

@skwde This worked for me to fix:

Subsequently, radian started for me as expected:

ps ax | grep radian
52731 s014  Ss+    0:01.91 <omitted>/MacOS/Python /usr/local/bin/radian --no-save --no-restore
skwde commented 8 months ago

@rfhb thanks for the suggestion but this doesn't solve the problem.

I tried re-adding as you suggested but the problem keeps reappearing. I also tried

"r.rterm.option": [
        "--no-save --no-restore"
    ],

Then I get

The terminal process "<path to base>/.conda/envs/r/bin/radian '--no-save --no-restore'" terminated with exit code: 2.

It looks like the problem is that the parameters are included inside ''.

It actually also fails when I set

"r.rterm.option": [],
The terminal process "<path to base>/.conda/envs/r/bin/radian" terminated with exit code: 1.
botsunny commented 7 months ago

Hey there, did you find a solution to your problem?

skwde commented 7 months ago

@botsunny Unfortunately no...

jondeans commented 7 months ago

I have nearly the same environment set up and the same issue.

It's odd because running radian --no-save --no-restore from the CLI works fine and you can reproduce the same errors manually by adding in the quotes radian '--no-save --no-restore' or radian '--no-save', '--no-restore'

Setting r.bracketedPaste: true like the install instructions doesn't seem to work either.

{
  "r.bracketedPaste": true,
  "r.rterm.linux": "/home/user/.local/bin/radian"
}

My hunch is that if the options from the r.term.option list could be passed differently this would all go away, but I'm not familiar enough with VS Code extensions to attempt that fix here.

However!

Adding your R binary to the options list does seem to work for now.

{
    "r.rpath.linux": "/home/ec2-user/miniconda3/envs/ngs/bin/R",
    "r.rterm.linux": "/home/ec2-user/miniconda3/envs/ngs/bin/radian",
    "r.rterm.option": [
        "--no-save",
        "--no-restore",
        "--r-binary=/home/ec2-user/miniconda3/envs/ngs/bin/R"
    ]
}
LucasMS commented 2 months ago

I hope this helps you guys.

A solution that I found was to work with conda + vscode + renv.

Each project that I work on has its own conda environments (stored at conda_envs), including one called r. I use renv to track and manage my R libraries. This is a bit different from managing your R libraries within conda. I work project-based, so each time I connect to a project, I open the VSCode in the project folder. I tell vscode to look for R in the project, so it always attaches the R of the project. Radian does not work (I have not tested it extensively though).

This is a reproducible set-up:

  1. At the project folder called project/, I have the r.yml file:
name: conda_envs/r
channels:
  - conda-forge
  - bioconda
  - defaults
dependencies:
  - r-base=4.1.2
prefix: conda_envs/r
  1. Install conda environment and R packages using the conda environment and renv
conda env create --prefix conda_envs/r -f r.yml
conda activate conda_envs/r
# Install packages using renv
# If you have conda_envs in .gitignore, do not need to do it.
echo "conda_envs" > .renvignore
R -e 'install.packages(c("renv"), repos = "http://cran.us.r-project.org")'
R -e 'renv::init()'
R -e 'renv::install(c("languageserver", "httpgd", "jsonlite", "rlang"))' # libs necessary for vscode
  1. Settings for vscode:
{
    "r.plot.useHttpgd": true,
    "r.rpath.linux": "${workspaceFolder}/conda_envs/r/bin/R",
    "r.rterm.linux": "${workspaceFolder}/conda_envs/r/bin/R" // radian will not work
}
  1. Go to the folder as base directory: File --> Open Folder...

  2. Open a new R terminal (I normally go to + on terminal tab and select R terminal

Still trying to figure out how to get radian working in this setup. I think the trick is to tell radian which R to use, for instance $ env R_HOME=/usr/local/lib/R radian. But I could not find a solution for my case where each project has its own R version.