Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...