RStudio cannot find commands using system()

Hi everyone

I have downloaded conda and installed packages using it that are available globally on any new instance of Terminal, but when I run:

system("conda -V")

I get the error:

sh: conda: command not found

This happens in R too, but not if I run R from inside an active Terminal window and then run the command.

Not sure I have ever encountered the problem before. Any suggestions?

MacBook Pro M2
MacOS 13.3.1
R v4.2.2
RStudio Version 2023.03.1+446

The command system() does open a shell, but not necessarily the same as the default shell of Terminal. If I'm not mistaken, on Mac OS by default the shell is zsh, here you can see system() called sh.

system() is a bit of a pain, and best avoided if possible, for conda is there a reason you're not using {reticulate}? This package can directly interact with conda, taking care of some details for you (and letting you specify the path to an existing conda installation, or reinstalling one for you).

Hi Alexis

Thanks for the response. I agree system() can be a bit of a pain. I am trying to use an R package which calls a non-R package I have installed on zsh using mamba, so its not actually my code I am trying to use.

Seems like by default system() calls sh and that is not possible to change: How to set shell used by system() to invoke bash scripts in R? - Stack Overflow

So not sure how to really overcome the problem at the moment...

UPDATE:

I changed the default shell to bash in RStudio Global Options, opened a Terminal and ran source <path to conda>/bin/activate followed by conda init bash.

This resulted in conda being available in my bash terminal and thought this meant I would get success.

However it still did not work. :frowning: , and when I run system(paste('echo $PATH')) the correct paths are not present. Frustrating.

So the solution I found was to copy the output of echo $PATH from the Terminal into my .Rprofile and then everything seems to work calling system(paste('conda -V') for example.

It seems that system() is called based on the paths known to R on start up, so I needed to add them to the .Rprofile as well.

Great that you figured it out. I think another approach (based on this answer, but that I can not test at this time) would be to call your normal shell, zsh if I'm not mistaken. Something like:

system(paste("/bin/zsh -c", shQuote("conda -V")))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.