7

I installed R from official cran website and i can run R from the Rstudio but when i try to use R from the terminal, I get the following results:

(base) ege@Eges-MBP ~ % R  
zsh: command not found: R
(base) ege@Eges-MBP ~ % RScript 
zsh: command not found: RScript

How can i enable the command R and run RScript from terminal on my mac?

Ege Can
  • 195
  • 1
  • 10
  • try opening R from the terminal within Rstudio (usually next to the R Console tab). if it works, type `which R` in also in the terminal within Rstudio *OR* look at the path that RStudio is using to find R under `Tools` > `Global Options` > `R version` right on top of the options window. Then you may either that path to your $PATH, or create a (symbolic) link from R binary to the folder where your binaries usually are. (maybe /usr/local/bin, I am not sure how it is in MacOS – Marcelo Avila Apr 01 '21 at 11:47
  • can u use R terminals other than `zsh`? – Marcelo Avila Apr 01 '21 at 11:50
  • Hi, i just switched to Mac from Windows so I am new to MacOS. I have typed `which R` to the terminal in Rstudio , it did not throw an error but i got nothing returned. Instead, i have used `R.home()` from terminal and it returned `/Library/Frameworks/R.framework/Resources` – Ege Can Apr 01 '21 at 12:05
  • 1
    great that you found a solution. Just to be a little clear, the `terminal` and `R console` in general and in RStudio are two different things. In RStudio, next to usual `Console` where you can interact your R enviroment, there is also a `Terminal` (and some other tabs, such as `Jobs´). This Terminal tab can be used just like a normal Terminal. It is a bit confusing, but you entered R.home() in the Console, not in the terminal. (it sounds pedantic, but the right terminology helps a lot finding the right questions and answers :) – Marcelo Avila Apr 01 '21 at 13:55

4 Answers4

9

This is possibly due to a bug with the current version of CRAN's R installation package (present as of version 4.0.5), that affects certain versions of Big Sur. I've answered this question here: R: command not found

In short, if running uname -r gives you a release number greater than 20, then you may need to manually create symbolic links that the package installer failed to.

In short:

    sudo -s
    mkdir -p /usr/local/bin
    cd /usr/local/bin
    rm -f R Rscript
    ln -s /Library/Frameworks/R.framework/Resources/bin/R .
    ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
Yuri Broze
  • 136
  • 3
5

I found a way to do this.

I first found the location of R and Rscript on my disk by running R.home() on my R console. Then, i followed this guide and copied the returned location from R.home() and pasted on a new line on /etc/paths.

Then, i restarted the terminal and i was able to use R and Rscript commands

Ege Can
  • 195
  • 1
  • 10
4

I had a similar issue. I was able to solve it by adding the below line (path obtained by running R.home() inside R) to my .zshrc. Thanks for the guidance.

export PATH="/Library/Frameworks/R.framework/Resources:${PATH}"
sandeep
  • 41
  • 2
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 27 '21 at 21:07
1

You need to add the directory where the R binaries are installed to the PATH environment variable.

You can add this in the current session as follows:

PATH=/usr/local/bin/:$PATH

To have this done automatically in every new session ensure that this line is added to your ~/.zshrc file:

export PATH="/usr/local/bin/:${PATH}"
Tom Kelly
  • 1,458
  • 17
  • 25