0

I know that this question is available in this page, but I have tried most of them, and I just can’t make it works. How can I set an external program as blast permanently using Sys.setenv for my personal functions in R

I’m using a MacOS with R4.1.1 and RStudio 2021.09.0.

The common way that I usually call an external program, in this case BLAST (ncbi-blast-2.2.29+) is just copy and paste the line in RStudio console, something like:

Sys.setenv(PATH = paste(Sys.getenv("PATH"), "/Users/myname/myprograms/ncbi-blast-2.2.29+/bin/", sep= .Platform$path.sep))

And then I just run my personal functions that use blast, and I have no problem with that. But I just want to set blast permanently in RStudio, and don’t need to copy and paste that line every time that I start RStudio. So I tried to paste that line in .Renviron, and then start up Rstudio and tried to run blast functions, and it didn’t work !!!

So I tried different, add the path in .zshrc (I’m using /bin/zsh), using nano or sublime text:

nano ~/.zshrc
export BLAST_PATH=$PATH:~/genetools/ncbi-blast-2.2.29+/bin/

And then just add the BLAST_PATH to Sys.setenv in .Renviron

nano .Renviron

Sys.setenv(PATH = paste(Sys.getenv("PATH"), "BLAST_PATH", sep= .Platform$path.sep))

or

Sys.setenv(PATH = paste(Sys.getenv("PATH"), BLAST_PATH, sep= .Platform$path.sep))

And restart RStudio and just it didn’t work when I use my personal functions !!!

Any way to set it permanently in RStudio ?

By the way, My .Renviron is located at home (~/.Renviron) and my working directory (R sessions ) in RStudio is located in ~/R !!!

Thanks

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
abraham
  • 661
  • 8
  • 14
  • 1
    Environment variables for a user has nothing to do with R. You need to set the variables in your pc. check https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables for example on how to do it in linux, and https://phoenixnap.com/kb/windows-set-environment-variable on windows – Onyambu Jun 03 '23 at 00:20
  • @Onyambu Except that these solutions categorically do not work for RStudio (or any other GUI applications) on macOS. – Konrad Rudolph Jun 03 '23 at 10:05
  • @KonradRudolph I specified that you do not necessarily need to use R. Setting environment variable has nothing to do with R. I am not sure about macOs. As far as I know, in windows, one cam simply use `setx` command, or just search for environment variables from the search button and set them the way they want – Onyambu Jun 03 '23 at 16:33
  • @Onyambu But as my answer shows *there is no good non-R way to do this on macOS*. You need R (specifically, `.Renviron`). So the question, for better or for worse, *does* have to do with R. – Konrad Rudolph Jun 03 '23 at 16:38

2 Answers2

3

.Renviron does not accept "R code". You can check what's happening for example with calling readRenviron("~/.Renviron"). You could follow this route inside .Renviron or just put your code (above) in an .Rprofile file either in your $R_USER or project directory, depending on your requirements, ie if you want to have those ENV variables "project specific" or not.
Details on the startup process can be found in the R manual or for example in the startup package documentation, [startup package] also adds some nice functionality in case you need to handle passwords or other things along the "way" [ie. the startup process].

GWD
  • 1,387
  • 10
  • 22
1

As mentioned, .Renviron does not expect R code but rather shell-like variable assignments. So you could add the following to your .Renviron:

PATH=~/genetools/ncbi-blast-2.2.29+/bin/:$PATH

You could also adjust your PATH inside .zshrc, but unfortunately RStudio will not see these values! Unless, that is, you are actually starting RStudio from inside your zsh, for instance via open -a RStudio. Only then will it work.

In general, applications only inherit the shell environment when they are launched from the shell, and .zshrc is only sourced by your zsh. This means that it cannot be used to set global configuration for applications on macOS. In earlier versions of macOS it was possible to configure the launch daemon (which is the background application that is launching all GUI applications on macOS). However, this no longer works because Apple disabled the mechanism.

Unfortunately, as far as I can tell, there is no way to configure the global environment for GUI applications on macOS any more. All the different hacks that are documented on Stack Overflow fail. The only way is via running launchctl setenv … at every startup.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • thanks so much to all, it just didn't work for me, but I will try again, thanks so much anyway !!!! – abraham Jun 11 '23 at 04:47