5

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?

MAPK
  • 5,635
  • 4
  • 37
  • 88
  • Does quime comes with R installed? – rpanai Apr 25 '19 at 17:04
  • Possible duplicate of [How to set up conda-installed R for use with RStudio?](https://stackoverflow.com/questions/38534383/how-to-set-up-conda-installed-r-for-use-with-rstudio) – rpanai Apr 25 '19 at 17:08

6 Answers6

10

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

Bruno
  • 4,109
  • 1
  • 9
  • 27
  • 2
    Thanks, but I am getting this error now : `> conda_list()[[1]][3] %>% + use_condaenv(required = TRUE) Error in use_python(conda_env_python, required = required) : Required version of Python '/home/owner/anaconda3/envs/qiime2-2019.1/bin/python' cannot be used because another version of Python ('/usr/bin/python') is already initialized for this process.` – MAPK Apr 25 '19 at 17:37
  • I never had this problem my suggestion would be to either try what Shivan is saying or restarting your R session, I would mention that I usually use python inside an Rmarkdown document so maybe it doesn't work as well on an R script – Bruno Apr 25 '19 at 17:45
  • a minor modification that: #Checking python `platform <- import("platform")` & `print(platform$python_version())` – Grec001 May 12 '22 at 23:56
  • `Error in use_python(python, required = required) : Specified version of python '~/miniconda3/envs/salmon/bin/python' does not exist.` – Emile Zäkiev Mar 20 '23 at 16:19
3

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"))
Macdonald
  • 43
  • 3
1

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

mmann1123
  • 5,031
  • 7
  • 41
  • 49
ShivamPR21
  • 119
  • 1
  • 8
1

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")
B--rian
  • 5,578
  • 10
  • 38
  • 89
鄭俊杰
  • 91
  • 1
  • 2
1

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.

Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
0

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')
kwat22
  • 11
  • 1