4

I am a bit lost when dealing with installing and using R. I installed R 3.0.1 from source and did the ./configure, make, make check, and make install as suggested. However I tried running R but it said that R wasn't in the /usr/bin folder. So I then copied the entire R-3.0.1/bin directory into my /usr/bin directory using cp. Now I'm getting a few errors regarding /usr/bin/env when trying to use RScript on a hello_world.R script I wrote from the O'Reilly R In a Nutshell book I store in a file hello_world.R the contents are below:

#! /usr/bin/env RScript
print("Hello World!");

Simple enough, but when I try to load it I get the following error:

$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory

I'm not sure if this is a PATH problem or something, but when I search in my /usr/bin directory I do see the RScript file in there along with (R, BATCH, and the others associated with R programming language). Any help is greatly appreciated. Cheers.

  • What OS/distro are you using? – Jake Burkhead Jul 05 '13 at 21:08
  • Ubuntu 13.04 (Raring) –  Jul 05 '13 at 21:12
  • Any reason in particular you are trying to install from source as opposed to `apt-get install`? – Jake Burkhead Jul 05 '13 at 21:18
  • I was trying to get Bioconductor, and according to their website the current version of Bioconductor needs R-3.0.1. From what I read using `apt-get install r-base` can only get you version 2.15.2 according to this [link](https://launchpad.net/ubuntu/+source/r-base). I originally had installed R-2.15.2 using `apt-get` but then read the Bioconductor site and tried to install the new version by source. –  Jul 05 '13 at 21:30
  • `make install` usually installs software in `/usr/local/`, to avoid interference with software installed by the package manager: copying files after installation usually brings problems. What is your `$PATH`? What do `which R`, `which Rscript` return? Is `/usr/local/bin/Rscript` executable? Can you run `R`? – Vincent Zoonekynd Jul 05 '13 at 21:31
  • 1
    in R what is the output of `Sys.which("Rscript")` ? – dickoa Jul 05 '13 at 21:39
  • I can run R. My $PATH is `echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games ` Which R returns `/usr/local/bin/R` and which RScript doesn't return anything. In R the `Sys.which("Rscript")` returns `"/usr/local/bin/Rscript"` –  Jul 05 '13 at 21:44
  • 4
    It is `Rscript`, not `RScript`... – Vincent Zoonekynd Jul 05 '13 at 21:51
  • 1
    That was the problem, it works with the lowercase 's', there must be a typo in the book. Thanks! –  Jul 06 '13 at 02:01

5 Answers5

6

You may be using an invalid command line option for Rscript in your shebang line.

For instance ...

#!/usr/bin/env RScript --vanilla

remove "--vanilla" (or other offending option) and rerun your script

#!/usr/bin/env RScript

I know you didn't put this in your example, but the solution may help others searching for the same issue.

Vyke
  • 3,099
  • 1
  • 17
  • 6
4

Again, the good solution to this problem is very simple and clearly explained in the man page of env. The script should use the env command to invoke Rscript and not Rscript directly:

#!/usr/bin/env Rscript

some R code now...

But a script like this will read the user's .Rprofile among other things. When we want to have a vanilla R session (in order to start with a clean and controlled R), we must pass the option --vanilla. If you try something like

#!/usr/bin/env Rscript --vanilla

some R code now...

env will take the string Rscript --vanilla a the command to execute and will inevitably return the error message

/usr/bin/env: ‘Rscript --vanilla’: No such file or directory

In env's man page, there is an option called -S for splitting the strings. Its role is exactly to solve the problem above and use the first string Rscript as the command name, and the following strings (like --vanilla) as options to pass to Rscript.

The solution is therefore:

#!/usr/bin/env -S Rscript --vanilla

some R code now...
David Bellot
  • 643
  • 7
  • 13
2

Put in the shebang line of your script #!/usr/bin/Rscript and it should work.

As a side remark if you want to keep up-to-date with the R versions from CRAN and not relying on the native R of your Linux distro (Ubuntu) then add the following line in your apt sources:

deb http://my_favorite_cran_mirror/bin/linux/ubuntu raring/

After that you can always use the apt system to install R which -I would agree with Jake above- it should be the preferable way to install R.

*Change the my_favorite_cran_mirror with a valid CRAN mirror that is close to you.

Knak
  • 496
  • 3
  • 14
Costas B.
  • 186
  • 12
  • 1
    You are Sadly Out of Luck if `Rscript` is not installed in `/usr/bin`. Which happens e.g. on MacOS 10.11 "El Capitan" for instance. A more portable solution is to use a shebang line such as `#!/usr/bin/env Rscript` because the location of `env` is way more standard among Unices. Yes, there are UNIX-like OS-es in addition to Linux... :-) – András Aszódi Feb 23 '17 at 15:43
  • I agree, sadly there are other "Unix-like OSes" that do not even allow admins to write to /usr/bin. – Costas B. Feb 25 '17 at 18:02
1
#! /usr/bin/env RScript
print("Hello World!");

Simple enough, but when I try to load it I get the following error:

$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory

Here u make mistake is that instead of RScript write Rscript. The syntax will be

#! /usr/bin/env Rscript
print("Hello World!");

Then run it it will work (y) all the best.

$./hello_world.R
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Yanish
  • 11
  • 2
1

I arrived at this question trying to understand this error message on a cluster computer where I did not have control over the R installation.

In general, when I converted Rscript in my makefile to /usr/bin/Rscript the error message no longer occurred.

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173