Content of script.r
:
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)
message(sprintf("Hello %s", args[1L]))
The first line is the shebang line. It’s best practice to use /usr/bin/env Rscript
instead of hard-coding the path to your R installation. Otherwise you risk your script breaking on other computers.
Next, make it executable (on the command line):
chmod +x script.r
Invocation from command line:
./script.r world
# Hello world
Try littler. littler
provides hash-bang (i.e. script starting with #!/some/path) capability for GNU R, as well as simple command-line and piping use.
Miguel Sanchez's response is the way it should be. The other way executing Rscript could be 'env' command to run the system wide RScript.
#!/usr/bin/env Rscript
#!/path/to/R
won't work because R is itself a script, so execve
is unhappy.
I use R --slave -f script
If you are interested in parsing command line arguments to an R script try RScript which is bundled with R as of version 2.5.x
http://stat.ethz.ch/R-manual/R-patched/library/utils/html/Rscript.html
This works,
#!/usr/bin/Rscript
but I don't know what happens if you have more than 1 version of R installed on your machine.
If you do it like this
#!/usr/bin/env Rscript
it tells the interpreter to just use whatever R appears first on your path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With