System("source activate condaenv")
On Wed, 20 Mar 2019 09:59:21 +0100
Sandra Elisabeth Chaudron <sandra.chaudron at gmail.com> wrote:
I am using the server version of RStudio and I have a script where I
want to activate the conda environment that I set up for a
bioinformatic tool called MinVar.
For that I use in my script the command: system("source
/home/ubuntu/miniconda3/bin/activate minvar").
Unfortunately, this is probably not going to work as is. In *nix-like systems, child processes cannot alter the environment variables of their parents, so when one wants a shell script to alter environment variables in the current session, they use "source". The reason your command returns an error message is probably because "source" is a command specific to /bin/bash, while R calls /bin/sh, which might be symlinked to /bin/dash instead of /bin/bash on Ubuntu. The POSIX-correct form would be ". /home/ubuntu/miniconda3/bin/activate minvar", that is, just a dot instead of the word "source". But the underlying issue would stay the same: R system() function launches a subprocess /bin/sh; the "source" or "." command causes the environment of the *child* shell process to be changed; environment of the *parent* R process stays unchanged. Your best bet would be to read the activate script, understand the changes to the environment it causes and use Sys.getenv() / Sys.setenv() to make equivalent changes in R environment variables.
Best regards, Ivan