-->

GoogleSearch



Scientist. Husband. Daddy. --- TOLLE. LEGE
외부자료의 인용에 있어 대한민국 저작권법(28조)과 U.S. Copyright Act (17 USC. §107)에 정의된 "저작권물의 공정한 이용원칙 | the U.S. fair use doctrine" 을 따릅니다. 저작권(© 최광민)이 명시된 모든 글과 번역문들에 대해 (1) 복제-배포, (2) 임의수정 및 자의적 본문 발췌, (3) 무단배포를 위한 화면캡처를 금하며, (4) 인용 시 URL 주소 만을 사용할 수 있습니다. [후원 | 운영] [대문으로] [방명록] [옛 방명록] [티스토리 (백업)]

이 블로그 검색

R: stand-alone R scpript using commandArgs()

라벨:


* http://cran.r-project.org/doc/manuals/R-intro.html#Scripting-with-R
* http://projects.uabgrid.uab.edu/r-group/wiki/CommandLineProcessingB.4 Scripting with R

If you just want to run a file foo.R of R commands, therecommended way is to use R CMD BATCH foo.R. If you want torun this in the background or as a batch job use OS-specific facilitiesto do so: for example in most shells on Unix-alike OSes R CMDBATCH foo.R & runs a background job. You can pass parameters to scripts via additional arguments on thecommand line: for example

R CMD BATCH --args arg1 arg2 foo.R &

will pass arguments to a script which can be retrieved as a charactervector by

args <- commandArgs(TRUE)

This is made simpler by the alternative front-end Rscript,which can be invoked by

Rscript foo.R arg1 arg2

and this can also be used to write executable script files like (atleast on Unix-alikes, and in some Windows shells)

#! /path/to/Rscript args <- commandArgs(TRUE) ... q(status=)

If this is entered into a text file runfoo and this is madeexecutable (by chmod 755 runfoo), it can be invoked fordifferent arguments by

runfoo arg1 arg2

For further options see help("Rscript"). This writes Routput to stdout and stderr, and this can be redirected inthe usual way for the shell running the command.

If you do not wish to hardcode the path to Rscript but have itin your path (which is normally the case for an installed R except onWindows), use

#! /usr/bin/env Rscript ...

At least in Bourne and bash shells, the #! mechanism doesnot allow extra arguments like#! /usr/bin/env Rscript --vanilla.

One thing to consider is what stdin() refers to. It iscommonplace to write R scripts with segments like

chem <- scan(n=24) 2.90 3.10 3.40 3.40 3.70 3.70 2.80 2.50 2.40 2.40 2.70 2.20 5.28 3.37 3.03 3.03 28.95 3.77 3.40 2.20 3.50 3.60 3.70 3.70

and stdin() refers to the script file to allow such traditionalusage. If you want to refer to the process's stdin, use"stdin" as a file connection, e.g. scan("stdin", ...).

Another way to write executable script files (suggested by FrançoisPinard) is to use a here document like

#!/bin/sh [environment variables can be set here] R --slave [other options] <
but here stdin() refers to the program source and"stdin" will not be usable.


Very short scripts can be passed to Rscript on thecommand-line via the -e flag.





라벨:





Scientist. Husband. Daddy. --- TOLLE. LEGE
외부자료의 인용에 있어 대한민국 저작권법(28조)과 U.S. Copyright Act (17 USC. §107)에 정의된 "저작권물의 공정한 이용원칙 | the U.S. fair use doctrine" 을 따릅니다. 저작권(© 최광민)이 명시된 모든 글과 번역문들에 대해 (1) 복제-배포, (2) 임의수정 및 자의적 본문 발췌, (3) 무단배포를 위한 화면캡처를 금하며, (4) 인용 시 URL 주소 만을 사용할 수 있습니다. [후원 | 운영] [대문으로] [방명록] [옛 방명록] [티스토리 (백업)] [신시내티]

-