Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running R from windows command prompt

I have a R program in a txt file say "functions.txt".
I load the "functions.txt" file the R using source("function.txt") and then call functions f1(), f2() etc. which are declared and defined within "function.txt" file.
I also need to load a couple of R libraries using library() before I can use f1(), f2() etc.

My question is can I acheive all this (i.e. calling function f1() and f2()) from the windows prompt without opening the R environment ?

So essentially I want to

  1. load the R libraries I need to run f1(), f2() etc.
  2. load the function.txt file
  3. run the individual functions f1() etc.
  4. record the result

all from from the command promt of windows c:\>

I have windows version of R installed in my computers.
It would be very kind of anyone to give a detailed answer as I am not very computer savvy.

Regards

like image 876
babu Avatar asked Apr 19 '11 17:04

babu


People also ask

How do I start R from command line?

Starting R If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon. You can also use this method on a *NIX system that has a window manager such as KDE.


2 Answers

Bart's post is correct, but this can be done simpler. If the code

f1 <- function() {   print("A") }  f2 <- function() {   print("B") }  f1() f2() 

is in a file 'myRcode.R'; then

Rscript myRcode.R 

will load and execute it, including the two function calls.

Rscript.exe is in the same directory as R.exe -- which one may have to add to the $PATH.

like image 67
Dirk Eddelbuettel Avatar answered Oct 11 '22 21:10

Dirk Eddelbuettel


The following "works on my machine" (not Windows though, but it should...):

If your functions.txt looks like:

f1 <- function() {   print("A") }  f2 <- function() {   print("B") } 

the command:

Rscript -e "source('functions.txt');f1();f2()" > out.txt 

should create the file out.txt containing:

[1] "A" [1] "B" 
like image 32
Bart Kiers Avatar answered Oct 11 '22 21:10

Bart Kiers