4

I'm trying to make helloworld.R run. I have given it executable rights and have added #!/usr/bin/Rscript at the top of the file. (I have also attempted #!/usr/bin/en Rscript). It is a relatively simple file:

#!/usr/bin/Rscript

x <- rnorm(100)
df <- data.frame(x = x)
write.csv(df, "sim_data.csv")

However, any attempt to run the file has been met with the following error

ARGUMENT '/home/path/to/file/helloworld.R' __ignored__

R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

(...)

Surprisingly, renaming the file to helloworld.py lets it run just fine (and outputs sim_data.csv with its expected content), but this seems hacky, so I was wondering if there was a better solution.

Frank Vel
  • 1,202
  • 1
  • 13
  • 27
  • This behavior would occur if the executable were R, rather than Rscript; is /usr/bin/Rscript actually a distinct executable, or a manually created link to R? – Martin Morgan Jan 09 '18 at 11:14
  • @MartinMorgan I have assumed they were the same thing. I don't think I have made any manual configurations after simply installing R, so it's likely that it's a distinct executable. – Frank Vel Jan 09 '18 at 11:20
  • please show the filesystem attributes of the file _and_ how you're calling it on the command line – hrbrmstr Jan 09 '18 at 12:33
  • It is run by means of double-clicking the file. – Frank Vel Jan 09 '18 at 12:48

2 Answers2

2

The error message indicates that it is the R executable and not the Rscript executable that was run. You can verify this by using a terminal:

R  /home/path/to/file/helloworld.R         # produces above error
Rscript /home/path/to/file/helloworld.R    # should work
/home/path/to/file/helloworld.R            # should work if file is executable

Most likely your window manager or desktop environment has .R files associated with the R executable and therefore starts R when you doulbe click such a file. How to change that depends on the window manager or desktop environment in use.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • I had the same issue but the executable file doesn''t run even if I choose the right app. My screen (black screen) close prompt(like 1 sec) and I can't figure it out why. I work on windows. – Mary Nastase Dec 03 '20 at 09:20
  • @MaryNastase I am not a (regular) Windows user myself. It is probably best to ask a separate question. – Ralf Stubner Dec 03 '20 at 10:14
1

You're not running RScript, you're running R, So ask R to tell you to run a file:

$  R --help

Usage: R [options] [< infile] [> outfile]
   or: R CMD command [arguments]

Options:
  --args                Skip the rest of the command line
  -f FILE, --file=FILE  Take input from 'FILE'
  -e EXPR               Execute 'EXPR' and exit

So try it this way:

R -f helloworld.R

The question is, why is your Rscript executable bugged and pointing to R?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • That's not how `Rscript` works on my system (version 3.5, 3.6 and dev on Linux). Why does `Rscript --help` show the output from `R --help`? – Ralf Stubner Aug 12 '19 at 08:00
  • Oh, that's it. So the problem behind the problem is: Rscript is just default points to R. The next question becomes: "Why is R installed but Rscript is not installed?" – Eric Leschinski Aug 12 '19 at 09:26
  • Which OS and which R version do you use? How did you install R? – Ralf Stubner Aug 12 '19 at 09:34
  • 1
    macOS Mojave, R version 3.3.2 installed with 'brew install r'. What had happened is my /usr/local/bin/Rscript got root permissions because I was fooling around with it. and after a new install it got bugged, so then Rscript defaults to `R`. What I did was `rm /usr/local/bin/Rscript` and then do a fresh install and now it works as you say. – Eric Leschinski Aug 12 '19 at 09:50