Tapper
Tapper

Reputation: 1443

Using R in Visual Studio Code with conda environment

I would like to use the R environment I installed with conda inside Visual Studio Code (on Macos). First I installed R with conda. But how do I use/activate the environment in Visual Studio Code? In the settings I can't find the equivalent to "Python: Select Interpreter" or "python.venvPath" Thanks!

Upvotes: 14

Views: 9097

Answers (4)

moi_taiga
moi_taiga

Reputation: 1

A workaround which works for me:

1. Make a script which runs R e.g.


# Check if the R executable exists in the activated environment
R_PATH=$(which R)

if [ -z "$R_PATH" ]; then
    echo "R is not found in the activated conda environment: $1"
    exit 1
fi

# Start R using the path from the activated environment
exec "$R_PATH"  # Execute R 

2. Set the R path for interactive terminals to your script

"r.rterm.linux": "/path/to/start_R.sh"

3. use Python: Select Interpreter to select your chosen conda environment.

Now when opening an R terminal it should use the correct conda environment.

Upvotes: 0

Siyuan Feng
Siyuan Feng

Reputation: 3

Detailed configuration steps similar to @Andrea's idea could be found at this post.

If you want to attach VSCode-R to your current R session (e.g., opened in a Conda environment), an additional step to modify the .Rprofile is needed.

Upvotes: 0

Andrea
Andrea

Reputation: 355

It has been 2 years since this entry and the extension still doesn't support conda environments.

For my configuration (I've R installed in a conda environment), I found a pretty painless work around:

  • open 'vscode'
  • install the extension and configure it as suggested using the conda paths for both R and, if you have it installed, radian
  • close 'vscode'
  • open a terminal
  • activate your conda environment
  • start vscode from your terminal using code

After this, everything seems to be up and running correctly. You can start an R terminal using the command palette and, as you run your code, you should be able to see all the information about the environment and namespaces as well as your plots.

Upvotes: 8

Hong Ooi
Hong Ooi

Reputation: 57686

R support in VSCode is handled by a 3rd party extension. The most popular one is R by Yuki Ueda and there is also R Tools by Mikhail Arkhipov

For both of these, you can change the R interpreter to use in the settings.

However, there is no built-in support for Anaconda, mostly because it isn't that popular or necessary in the R community. Most people use the standard R installation instead and most help resources are written for that type of installation: https://cloud.r-project.org/bin/macosx/

Upvotes: 7

Related Questions