14

I'm working with R for a while, and I always worked with Rstudio, I tried just now to run a Rscript command in terminal (I have a mac..) and I got this error-

>Rscript script.R
 -bash: Rscript: command not found

when I tried to open R in the terminal I go the same error-

>R
-bash: R: command not found

I can run R code with the Rstudio and the R application, but I know there is a way to run R throw the terminal. Did I miss something when I installed R on my computer? do I need to add R to my PATH?

thanks in advance!

Phil
  • 7,287
  • 3
  • 36
  • 66
T.G.
  • 743
  • 3
  • 6
  • 27
  • 3
    This sounds like R is not on your PATH. – Tim Biegeleisen Jul 19 '16 at 10:40
  • @T.G. This is for sure, try to install it again , one solution ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew tap homebrew/science brew install r –  Jul 19 '16 at 10:45
  • Set the path for `Rscript.exe` in the environment variables to run the script through terminal. – Sowmya S. Manian Jul 19 '16 at 10:46

2 Answers2

11

Steps to run R script through Windows command prompt

  1. Set the PATH variable for Rscript.exein the environment variables. Rscript.exe can be found inside bin folder of R. Set the path for Rscript.exe to use Rscript command in Windows command prompt. To check if Rscript.exe has been set environmentally or not, type Rscript in command prompt. The follwoing message should come.

enter image description here

  1. Go to Command Prompt, set the path where your .R file is there.
  2. Run the following command: Here abcd.R is present under Documents folder. So I set path and then run Rscript abcd.R

enter image description here

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
4

For those who stumbled upon this but use a mac, you might find this useful. I recently downloaded and installed R and RStudio through the CRAN site. I didn't do it through homebrew. Since I downloaded this install directly from the site, it DID NOT add the RScript executable to my /usr/local/bin directory.

I have locate on my mac so I did a quick lookup:

locate RScript

And I found it here:

/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript

What I had to do was create a symbolic link to my /usr/local/bin directory to get it to work:

cd /usr/local/bin
ln -s /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript Rscript

Now I'm able to run Rscript through the command line. This may help someone else out there.

Paul Carlton
  • 2,785
  • 2
  • 24
  • 42