Achal Neupane
Achal Neupane

Reputation: 5729

How to activate anaconda environment within R

I have qiime2 program https://qiime2.org in my directory /home/owner/anaconda3/envs/qiime2-2019.1. In Linux terminal, I run source activate /home/owner/anaconda3/envs/qiime2-2019.1 to start this program.

I tried doing this within R studio as system('source activate /home/owner/anaconda3/envs/qiime2-2019.1'), but it it gives me this error: sh: 1: activate: not found Warning message: In system('activate /home/owner/anaconda3/envs/qiime2-2019.1') : error in running command

Is there a way to activate anaconda env within R or Rstudio?

Upvotes: 6

Views: 19783

Answers (6)

kwat22
kwat22

Reputation: 11

I ran into this issue when trying to run gdal commands through an R system call. I found that I needed to use the shell() function rather than system() to call my command. Once conda is initialized in a cmd.exe (by running conda init), the shell(cmd) call worked successfully through R.

shell('conda activate gdal-env && gdalwarp -s_srs EPSG:6339 -t_srs EPSG:6414 in.tif out.tif')

Upvotes: 0

Macdonald
Macdonald

Reputation: 53

I know this is an old question, but found some ways one can activate. One is using the library reticulate mentioned above, but I use it this way:

library(reticulate)
use_condaenv("py2_env")    #activate an environment e.g py2_env
base::system(paste0("sh py2_env_program_to_run.sh"))   #run a program that requires that environment

With reticulate im not sure though how you would deactivate the environment.

The second way, I wrote a little bash script called 'env_eval.sh'

#!/bin/bash

PATH=/opt/conda/bin:$PATH
export PATH

eval "$(conda shell.bash hook)"

Then I reference it in my R script everytime i want to use conda e.g:

base::system2(paste0("sh ",run_folder,"/code/bash/env_eval.sh conda activate py2_env && run_py2_env_program.sh"))

I can even deactivate a conda environment with this method:

base::system(paste0("sh ",run_folder,"/code/bash/env_eval.sh conda deactivate && run_program_that_should_not_be_in_any_conda_env.sh"))

Upvotes: 4

ShivamPR21
ShivamPR21

Reputation: 119

Generally, I do not use Rstudio but from some searches, I can suggest you try by setting python path instead of activating environment by Conda activate.

You can select which python interpreter you are going to use and here it.

    library(reticulate)    
    path_to_python <- "/anaconda3/envs/qiime2-2019.1/python"    
    use_python(path_to_python, required = TRUE)

here are some answers for the same type of question:

1> https://stackoverflow.com/a/54813273/9071644

2> https://stackoverflow.com/a/45891929/9071644

3> https://stackoverflow.com/a/43411909/9071644

Upvotes: 1

Bryan Butler
Bryan Butler

Reputation: 1850

If you want to use Python in RStudio, the best way to get it going is to create a separate 'reticulate' environment using Anaconda.

Part of the reason is that so you can use RMarkdown for your output requires PyQt5 which will break your Jupyter/Spyder environments if you overwrite PyQt.

Then you have to make an .Renviron file like this setup. which points R to the proper Python env. Otherwise, the default for RStudio seems to be a miniconda environment.

Once your separate reticulate environment is set and you have .Renviron pointing to it, all of your Python package installs should go into that environment.

Upvotes: 1

鄭俊杰
鄭俊杰

Reputation: 111

I think it could not be successful to enter the conda environment in R console, but you still can use the environment command by indicating the path.

For instance, my path of qiime is /home/username/miniconda3/envs/qiime2-2019.7/bin/qiime. If you want to run the code like qiime info, you can use the command:

system("/home/username/miniconda3/envs/qiime2-2019.7/bin/qiime info")

Upvotes: 1

Bruno
Bruno

Reputation: 4150

Yes there are multiple ways I recommend looking into the reticulate package but basically, R Studio preview 1.2 is capable of "finding" your conda environments.

My prefered way is:

library(reticulate)
library(tidyverse)

# Seeing your enviroments
conda_list()

#Using it
conda_list()[[1]][1] %>% 
  use_condaenv(required = TRUE)

#Checking python

import platform
print(platform.python_version())

Links

Reticulate: https://rstudio.github.io/reticulate/

My blog: https://twosidesdata.netlify.com/2019/03/23/exploratory-data-analysis-basic-pandas-and-dplyr/#how-to-set-up-reticulate

Upvotes: 11

Related Questions