Susan
Susan

Reputation: 539

Activate Conda environment inside R script

I am new to R coding.

I want to run an R script called from a Python script.

The Python script will use a Conda environment, env1, while the R script will use a different Conda environment, env2, in Linux.

So, I activate env1 before running the python script: conda activate /condaenv/env1/

Then I run the python script python testpy.py (this python script will call the R script testr.R).

My python script (testpy.py) is as follow:

import subprocess
subprocess.call(['Rscript','testr.R','hello'])

My Rscript testr.R will be as follows:

#!/condaenv/env2/bin/Rscript
library(Peaks)
library(httr)

I want to import all the R library inside the testr.R script from /condaenv/env2.

But I don't know how can an R script activate a Conda environment (env2) and will the R script run using the package installed in env2?

Upvotes: 0

Views: 873

Answers (1)

margusl
margusl

Reputation: 17304

A note regarding the title ("Activate Conda environment inside R script"), just as you activate python env before executing your the python script, R environment should be activated before invoking the R script.

Setting up 2 conda enviruonments and using conda run for executing Python script and invoking R from Python:

Conda envs
# base python env:
conda create -n conda_python -c conda-forge python
# base r env + httr:
conda create -n conda_r -c conda-forge r-base r-httr
Test scripts
# testpy.py

import os
import subprocess
print('CONDA_PREFIX (py) :', os.environ['CONDA_PREFIX'])
# subprocess.call('conda run -n conda_r Rscript testr.R', shell=True)
# or
subprocess.call(['conda', 'run',  '-n', 'conda_r', 'Rscript', 'testr.R'] )
# testr.R

message("-- R --")
message("CONDA_PREFIX (R)  : ", Sys.getenv("CONDA_PREFIX"))
message("R .libPaths()     : ", .libPaths())
resp <- httr::GET("https://api.stackexchange.com/2.3/info?site=stackoverflow")
message('Questions in SO : ', httr::content(resp, as ="parsed")$items[[1]]$total_questions)
Executing testpy.py

(when conda_python is not currently activated environment)

# conda run -n conda_python python testpy.py
CONDA_PREFIX (py) : /home/marguslt/miniconda3/envs/conda_python
-- R --
CONDA_PREFIX (R)  : /home/marguslt/miniconda3/envs/conda_r
R .libPaths()     : /home/marguslt/miniconda3/envs/conda_r/lib/R/library
Questions in SO : 23249251

As an alternative, using conda nested activation :

py script replaced with:

# testpy_noconda.py

import os
import subprocess
print('CONDA_PREFIX (py) :', os.environ['CONDA_PREFIX'])
subprocess.call(['Rscript', 'testr.R'] )

Activating both environments with --stack option and running testpy_noconda.py:

(base): conda activate conda_python
(conda_python): conda activate --stack conda_r
(conda_r): python testpy_noconda.py
CONDA_PREFIX (py) : /home/marguslt/miniconda3/envs/conda_r
-- R --
CONDA_PREFIX (R)  : /home/marguslt/miniconda3/envs/conda_r
R .libPaths()     : /home/marguslt/miniconda3/envs/conda_r/lib/R/library
Questions in SO : 23249325

Environment:

(conda_r): env | grep CONDA_
CONDA_EXE=/home/marguslt/miniconda3/bin/conda
CONDA_PYTHON_EXE=/home/marguslt/miniconda3/bin/python
CONDA_SHLVL=3
CONDA_PREFIX=/home/marguslt/miniconda3/envs/conda_r
CONDA_DEFAULT_ENV=conda_r
CONDA_PROMPT_MODIFIER=(conda_r)
CONDA_PREFIX_1=/home/marguslt/miniconda3
CONDA_PREFIX_2=/home/marguslt/miniconda3/envs/conda_python
CONDA_STACKED_3=true

Upvotes: 1

Related Questions