0

I am calling a R function from R studio like below

source("test.R)
test()

I now want to call this using Unix shell script.

Please let me know how to achieve this. Thanks.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Vinoth
  • 29
  • 1
  • 7

3 Answers3

6

"The Unix way" to do this is to add a first line in the so-called shebang style

#!/usr/bin/env Rscript

to the file test.R, and to follow this with

chmod 0755 test.R

to make it executable. Then you can just say

./test.R

and you created a new command. As you are on Unix you may also like our littler alternative to Rscript which you can install from CRAN, or use from your distro (ie Ubuntu or Debian)

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
5

Rscript -e 'source("test.R"); test()'

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
1

You could always write another script, newScript.R, that contains:

source("test.R") test()

and then from the command line, you can run:

Rscript newScript.R

jmartori
  • 384
  • 1
  • 5
  • 14