positron icon indicating copy to clipboard operation
positron copied to clipboard

Support R interpreters installed via conda

Open juliasilge opened this issue 1 year ago • 3 comments

Discussed in https://github.com/posit-dev/positron/discussions/4385

Originally posted by ami-null August 18, 2024 From my testing, Positron does not detected R interpreters installed in conda environments. Is it not supported? If not supported, is it planned?

This may be especially important for folks coming to Positron as Python conda users who want to use a little R.

juliasilge avatar Aug 19 '24 16:08 juliasilge

Related to #3724

juliasilge avatar Aug 19 '24 16:08 juliasilge

Maybe we could support an escape hatch via #2235.

juliasilge avatar Aug 19 '24 20:08 juliasilge

Re: the connection to #2235. I suspect (but have not done the research) that safely using a conda installed R goes beyond identifying the interpreter and also includes doing something about package libraries and repositories. As a non-conda user who has seen a lot of confused issues from conda users who are using R, success depends on committing fully to the conda life and not, for example, trying to use binary packages from CRAN.

jennybc avatar Aug 19 '24 22:08 jennybc

Came up again in https://github.com/posit-dev/positron/discussions/4498

juliasilge avatar Aug 27 '24 16:08 juliasilge

Just to follow up on your comment @jennybc, I completely agree that there are other related issues, and that a full conda workflow does incur some changes in workflow. However, I and many others are working on making viable workflows that are supported by conda. And Positron is currently in a position to be the IDE of choice for such workflows. These are the issues I have found, and what is being done currently to address them (that I know of):

  1. Not all CRAN packages are currently on conda-forge. However, most are - there's an amazing group of people doing the amazing work of packaging R packages there. @wolfv from Prefix has been hard at work to make it much easier to add the missing packages to conda channels (repositories) with rattler-build, see https://github.com/wolfv/r-forge. I think a new update of rattler-build that eases this process is not far away.
  2. Package installation from command line. The conda workflow differs from the normal R workflow in that packages are installed from the command line rather than the R console. To address this, I have started a package, {rpix}, which wraps pixi for use within R. And as such, dependencies for both R and Python can be handled with a single tool rather than with conda and renv separately.
  3. Set the correct library. Libraries of course need to be selected for the project. That's where renv has been great, as it's easy to localise the packages inside the project itself! pixi takes the same approach as renv, placing dependencies within each project rather than externally like other conda tools. If Positron could automatically identify a .pixi folder to locate dependencies, that problem would be (partially) solved. As an intermediate solution, I have made a project template that automatically sets the correct R library amongst other R-related goodies, which is then chosen when opening Positron from inside the project.

I'm very happy to help where needed if you need some outside input on conda workflows. Seeing this issue, #3724 and #2659 would, from my point of view go a long way to enable conda-based workflows.

roaldarbol avatar Aug 28 '24 13:08 roaldarbol

Another +1 for this issue -- I'm not big on installing R globally in my system environment, but I'm happy to conda install r-dplyr r-arrow for example in a conda environment, but I have to add the R interpreter this installs in the Positron settings UI to get it to show up as an available interpreter.

wesm avatar Nov 11 '24 07:11 wesm

Using Conda would enhance reproducibility, speed, and simplify environment management in Positron IDE. Coming from a Python data science background to R, I find RStudio's integration with Conda severely lacking, especially when compared to the smooth experience of Conda in VS Code for Python.

In my workflow, the streamlined integration of Conda is essential—something both the VS Code R extension and even PyCharm R extention (despite its bloat) attempt to support (better experience with pycharm). Adding Conda support in Positron IDE would make it a powerful choice for data scientists and developers, offering a lightweight yet effective environment for both R and Python users who rely on Conda.

aradar46 avatar Nov 12 '24 09:11 aradar46

Also +1 for this issue as well.

agm-eratosth avatar Nov 12 '24 18:11 agm-eratosth

As an interim solution on Linux, you can switch to a Conda environment in which R is installed via the Python interpreter selection dropdown menu. This will activate the Conda env and lets you land in the Python console. Switching now back to R console, hitting shutdown and then restarting R, will in turn restart ark. If you have hooked in using the code snipped below, it detects the former activated conda environemt and re-activates it before executing the ark kernel. In order to make this work, You simply need to move ark binary to ark.x and store the following as executable script named ark. Btw, this also works on remote connections if ark binary under ~/.positron-server has been replaced accordingly. If you'd like to automate this, you could check for POSITRON env variable and installation directory readlink -f /proc/$PPID/exe within ~/.bashrc.

#!/usr/bin/env bash
if [[ -n ${CONDA_PREFIX:+x} ]]; then
    source "${CONDA_EXE/%conda/activate}" $CONDA_DEFAULT_ENV
    [[ "$(command -v R)" =~ "$CONDA_PREFIX" ]] && export R_HOME="$CONDA_PREFIX/lib/R" || export -n R_HOME
fi
exec "$(dirname "$0")/ark.x" "$@"

koriege avatar Nov 15 '24 12:11 koriege

The discussion in #5740 highlights that our typical workaround for R installed via conda today:

cd project
conda activate myenv
positron .

is not workable for remote development using the remote SSH feature; AFAIK the way the remote SSH feature works doesn't let you activate an environment before starting the app.

juliasilge avatar Dec 18 '24 21:12 juliasilge

The discussion in #6600 highlights that our suggested workaround for using R with conda also does not work with WSL remote sessions.

juliasilge avatar Mar 04 '25 16:03 juliasilge

With PR #6888 I can get sth like this on my local machine: image

But I didn't get to test it on a remote server/WSL. Any suggestions?

kv9898 avatar Mar 20 '25 14:03 kv9898

Another +1 for this. My team and myself often need to work with many different packages that tend to have version conflicts with each other or require specific R versions. Conda helps us to navigate this issue by allowing us to have separate R installations for different projects and to manage those easily. Supporting these out of the box on remote servers would be great!

Edit : Just found out about the experimental feature. It seems to work great so far (at least locally).

SamuelMathieu-code avatar Jun 04 '25 00:06 SamuelMathieu-code

+1 for R in conda. It's becoming increasingly common that projects rely on both R and Python. It's very nice to have both in the same project environment. I'll check out the experimental feature.

brettmelbourne avatar Oct 15 '25 00:10 brettmelbourne

I'll add this info here so it is easier for folks to find. To turn on the very experimental, not-fully-supported support, add this to your settings.json:

"positron.r.interpreters.condaDiscovery": true

None of the IDE support that involves installing packages will work correctly for conda, and we believe there are problems with the functionality that involves interacting with R package installations at all. YMMV!

juliasilge avatar Oct 15 '25 00:10 juliasilge

In https://github.com/posit-dev/positron/issues/10359 we've got folks experiencing package installation problems with R installed via conda, as expected.

juliasilge avatar Nov 04 '25 18:11 juliasilge

In #10359, it sounds like the conda environment isn't being activated properly within Positron and so the other tools (like make/gcc) installed into the conda environment aren't being found -- so if you install packages from source in the terminal after running conda activate $ENV it works, but not from within Positron. I still think that supporting conda-forge-based R environments out of the box would be pro-user and healthy for the ecosystem.

wesm avatar Nov 06 '25 19:11 wesm

I think that conda R consoles should run in a process that does conda activate $ENV prior to loading R. (This is exactly the same mechanism we need to use for environment modules: #8075)

jmcphers avatar Nov 07 '25 00:11 jmcphers