22

I've searched the web, and I'm still unclear on how to run R from the Mac terminal. I have Rstudio and the standalone R app installed. I thought I could just type "R" from the command line as I do with "python", but that doesn't work. Is it necessary to edit the PATH in my bash profile? If so, how do I give the correct location of R?

Thanks for any help

Edits after receiving comments

So, I'm running Sierra, and when I type "r" or "R" at the terminal, I get "-bash: R: command not found." If I type, "which R" in the terminal I do not get any output.

Here is the output from "echo $PATH": /usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0@global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin

As for the installation, I believe I downloaded it directly from cran.r-project.org a while ago. I can locate the GUI in my applications and open it-- I have version 3.13. Is it possible, I only have R.app installed but not R? Perhaps that's the reason I'm getting the 'command not found' when typing "R" into the terminal?

Generally, I've been working in RStudio, but I'd still like to access R from the terminal and also to find where things are located. I'm fine with removing and re-installing R if it's easiest to start from square one. I hope the extra detail helps, and I appreciate the responses.

Community
  • 1
  • 1
sjc725
  • 491
  • 2
  • 5
  • 14
  • 2
    use Rscript yourscript.R – Vitalijs Jun 02 '17 at 19:47
  • 1
    It depends where/how you installed it. If you used **homebrew**, it will be `/usr/local/bin/Rscript your script.R` – Mark Setchell Jun 02 '17 at 20:08
  • 1
    I can launch R in terminal with either `r` or `R`, that is, both upper- and lowercase command work. Try `which R`. What does it return? And are you under Sierra? I had [some problems with R](https://stackoverflow.com/a/44238751/7843120) after I upgraded to Sierra and then upgraded R. –  Jun 02 '17 at 21:32
  • 1
    You have provided no information that will enable folks to help you. R runs fine on macOS both at the app and terminal levels. Where did you get R from? How did you install it? What does your `PATH` look like? Did you look to see if it is in `/Library/Frameworks/R.framework/Resources/bin/R` and if there's a symlink to `/usr/local/bin/R`? RStudio likely found it (you did not mention that key fact, though). Where does it say R is? – hrbrmstr Jun 03 '17 at 10:33
  • @hrbrmstr I agree with hrbrmstr's comments sjc725. Would you kindly add more details to your question by clicking the "edit" word below your question. Add what you can. Are you entering R in Terminal or the other apps would help me understand better. If you have R working now, do let others know so we know your issue is solved. – Cam_Aust Jun 03 '17 at 12:03
  • sjc725, I would value hearing if the new answer I have posted helps resolve your issue and question. The additional information you provided did help. Thanks. – Cam_Aust Jun 06 '17 at 08:11
  • sjc725, thanks for posting a comment of outcomes. Very helpful. I will likely delete my comments above as tidy up as most no longer needed. I find using this site quite a learning curve. Seems you are doing well on that front. – Cam_Aust Jun 13 '17 at 03:25
  • @hrbrmstr hi, I am looking at this old post and when I try to run R from terminal it only shows weird results(something I entered the terminal before). I checked the path of my R it appears to be under /usr/local/bin/R ... should it appear elsewhere? – JoZ Mar 24 '21 at 16:55

6 Answers6

26

An answer for those not that familiar with Terminal and Bash.

I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.

I found this latest install version 3.4.0 installs R for access in Terminal, and also installs R.app as part of the package.

To my understanding, reading support docs, if you have an older version of R it will update that. However it will not update an installation of R installed by the anaconda package.


Where are the R files stored?

I can only assume that with a fresh install of the latest R, R will work for you in Terminal.

To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:

>R.home()


In my case as example:

In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):

R.home()
[1] "/Library/Frameworks/R.framework/Resources"

In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.

R.home()
[1] "/anaconda/lib/R"

So I have two different versions of R, and Terminal accesses a different version to R.app.


How can I ensure I access the same version in Terminal as I do in the R.app?

For someone familiar with bash, and how the whole bash command system works I am sure there is a well constructed command. All the same here are some novice solutions.

-

• First Solution:

I could update the anaconda version, however, I would prefer not to as as other elements of the anaconda package my depend on this older version of R. For those not yet familiar with Terminal and bash, not such a novice solution.

-

• Second Solution:

This solution came from mko. It provides a single use solution. From the result above, and checking the directory structure a little further to find this R file.

icon in finder

Finding the significant R file enables me to edit an extension of the above path shown in the R.app. So add /bin/R to enter

/Library/Frameworks/R.framework/Resources/bin/R

Entering and pressing return will start R from this version.

Alternatively, one can find this file and icon in the GUI Finder, lead by the above result, and just double click on it, and it will open Terminal and a session with R running for you. Easy!

One could also make an alias of it and put it on your desktop for easy future starts.

-

• Third Solution:

My last solution I think may be best, adding to mko's solution. Make an alias.

Being in my home directory in Terminal I open .bash_profile using the nano text editor. (If you do not already know how to do this, then best not use this solution.)

I then add the line in this env file.

alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'

I then save the changes and exit this terminal session. I then open a new Terminal window. (This is so the changes to the env above are incorporated in the new terminal session).

Then when I enter the alias:

Rv340

The version of R I want opens.

You can choose a different alias name to "Rv340".

-

• Fourth Solution:

A second more permanent solution for opening the same version of R in Terminal is as follows.

Copy the path as showing in R.app in response to the R.home() command above, and add that path to PATH in your .bash_profile. (If you do not know already how to do this, then ignore this solution.) Do so as follows.

export PATH="/Library/Frameworks/R.framework/Resources:$PATH"

To my understanding, this ensures that bash looks here for R (and anything else), then moves on to the other paths in PATH. Since this adds this path to the beginning of $PATH, an env variable, bash looks here first where it finds the newer version first, and stops looking.

When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.

About PATH.

How to correctly add a path to PATH.

This solution may muck with anaconda's invocation of R. I have yet to check this.

Cam_Aust
  • 2,529
  • 2
  • 19
  • 28
  • 1
    After adding what you suggested-- export PATH="/Library/Frameworks/R.framework/Resources:$PATH" to my bash_profile, I can now successfully start R from the terminal. – sjc725 Jun 12 '17 at 19:01
  • @Cam_Aust: I have an R library (`evently`) which is installed here: `/Library/Frameworks/R.framework/Resources`. But I also have the same directory (evently) in /Documents folder which I got from github. Do you know how I can force R to run files from this local directory (/Documents)? – mOna Mar 25 '23 at 22:36
  • 1
    @mOna Other than 4 above, rewriting path to your documents folder, or making that folder your working directory in R. I have struggled with paths with a recent migration to a new laptop and currently not focused on such things, and consequently not so sure of my understandings. Others more into coding can likely answer better. If not, can I suggest contact the maintainer of the package (evently) which seems to be given in this doc [link](https://github.com/behavioral-ds/evently/blob/master/DESCRIPTION) You will find a contact email address in this doc on github. – Cam_Aust Mar 26 '23 at 10:27
  • @Cam_Aust: I'll try to contact them. Thank you! – mOna Mar 26 '23 at 18:23
13

First of all, you have to start terminal application. You can use either built in Terminal.app, or you can use replacement. My favorite one is iTerm2

https://www.iterm2.com

Then, you simply open terminal window and run R. Just like shown below:

enter image description here

Have fun with R!

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • Nice! Complements the long answer. – Cam_Aust Jun 03 '17 at 11:58
  • 4
    I think the question says that "R" cannot be found on command line, whether Terminal or iTerm. Saying "Open terminal and type R then RET" won't solve this problem. – Paul Apr 23 '21 at 19:53
  • Have you seen that there was the update to question after people posted answers ;) Instead of downvoting the whole internet, just add something that solves the issue ;) – Oo.oO Apr 23 '21 at 22:27
2

Just ran into the same issue when installing R-4.0.3.pkg on my MacBook (MacOS BigSur). Can open R.app to the clunky R GUI, but typing in 'R' in terminal doesn't work.

Turns out, an R executable lives here: /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R

So I added this alias to my newly created .zshrc script:

alias R '/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R'

Now when I type in R, it opens... I swear this all happened seamlessly in earlier versions.

Dan H
  • 29
  • 1
  • I think we need to have the equality sign after `R`, i.e. `alias R='/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R'`. – Cm7F7Bb Dec 28 '20 at 20:27
  • This is absolutely not necessary unless your R installation or your `PATH` is broken. Fix that instead of creating hacky workarounds, since it will have other negative consequences. – Konrad Rudolph Apr 21 '21 at 19:24
1

There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package. This only impacts MacOS system releases of 20 and above (you can check with uname -r).

I've included more info here, along with what the "correct" fix should be: manually creating symbolic links to /usr/local/bin that point to the R and Rscript binaries themselves. If this is the current challenge, then this would be a far better solution to creating aliases or manipulating PATH in various ways, since it's what the installation package intended to do (and presumably will again soon).

R: command not found

In short, if this is the problem, then Ashkan Mirzaee's answer (https://stackoverflow.com/a/67202173/2093929) to create the symbolic links directly is correct in form, but might not have the right link command. The 4.0.5 package intends instead to use:

    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
  • I just installed R version 3.6.3 from the CRAN binary package and the symlink was not created automatically. This solution worked for me – Johann Sep 21 '21 at 08:59
0

You can create a symbolic link from R and Rscript binaries to /usr/local/bin to add them to the PATH:

sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/R /usr/local/bin
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript /usr/local/bin

Now which R should return /usr/local/bin/R and you can use R.

Ashkan Mirzaee
  • 300
  • 4
  • 14
  • This is absolutely not necessary: installing R (both via the official package and via Homebrew) generates these links automatically. If they don’t exist your installation is broken and you should reinstall R from scratch rather than hacking around in it. Additionally, you shouldn’t link hard-coded versions into `/usr/local/bin`. Link the `Versions/Current` symlinked path instead. – Konrad Rudolph Apr 21 '21 at 19:21
  • The official package version 4.0.3 does not create the link. Anyway the command not forcing the link and will be ignored if the link/file is already exist. Fixed the hard-coded version. Thanks! – Ashkan Mirzaee Apr 21 '21 at 19:35
  • I just checked: the official R bundle installer 4.0.3 *does* create the symlinks as part of its post-flight build step. If it doesn’t, there’s something broken on your system. – Konrad Rudolph Apr 21 '21 at 21:15
  • Did you uninstall the previous R and remove /usr/local/bin/R and /usr/local/bin/Rscript? You might have the link from the older version. – Ashkan Mirzaee Apr 23 '21 at 21:48
  • No, I checked the installation bundler source. – Konrad Rudolph Apr 23 '21 at 22:45
-1

An easy way to open RStudio with admin privilege on macOS:

  1. Go to Applications, then right click on RStudio
  2. Select "Show Package Contents"
  3. Go to Contents/MacOS
  4. Now open terminal(in bash mode). Type sudo and drag the RStudio.exec into terminal and press on ENTER

Now RStudio will have admin access!