edisonying wrote: > > I am a beginner in R and know only a little about it yet. I have a script > written in R language, named as "a.txt" for example. I am using a Linux > machine, at present I only know that I can type "R" in the terminal and > then copy-paste the content in "a.txt" to the R's interface to execute the > program. However, I want to know if there is any method that allows me to > add some input arguments directly after the "R" in the terminal. > Specifically, I want to type the following in the cmd line: > > R (some flags or option) a.txt > > then the program will begin to run. Besides, if the program will read a > data file first, can I also specify the data file in the command line? > Then the complete command will become: > > R a.txt data.txt > > This is important for a beginner. Thanks very much!:working: > >
We usually have R execute scripts in the following manner: R --vanilla --slave --args [your args here] < scriptFile.R Inside scriptFile.R the arguments can be retrieved by using commandArgs(trailingOnly = T). The trailingOnly flag causes only the arguments following --args to be be returned and not --args, --vanilla (no save, no restore, quick startup) and --slave (makes R run quiet). Personally, I get tired of typing R --vanilla --slave --args all the time and prefer to use Rscript. Since you are using Linux you can do the same by putting the following hashbang at the top of your file: #!/usr/bin/env Rscript Then your script can be run using: ./scriptFile.R [your args here] The arguments are still accessed inside the script using commandArgs(T) Good luck! -Charlie ----- Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/how-to-run-a-R-program-with-input-arguments-tp24465852p24471559.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.