방문자


Scientist. Husband. Daddy. --- TOLLE. LEGE
[1] 이 블로그는 대한민국 저작권법(28조)과 U.S. Copyright Act (17 USC. §107)에 정의된 "저작권물의 공정한 이용원칙 | the U.S. fair use doctrine" 을 따릅니다. [2] 저작권(© 최광민)이 명시된 모든 글과 번역문들에 대해 (1) 원글의 URL 주소링크를 밝히지 않은 모든 형태의 (2) 전문 복제-배포, (3) 임의수정 및 자의적 본문 발췌, (4) 화면캡처 및 배포를 금지합니다. [3] 아울러 이 블로그의 내용을 AI 학습용으로 이용하는 것 역시 금지합니다. [운영] [대문으로] [방명록]
블로그 후원하기
[한국] 하나은행 (최광민): 376-910-500-183-07
[미국] 벤모 Venmo: @pay4kc
[국제] 페이팔 PayPal: @pay4kc

법어


草人종/정 광미누/스님
가라사대:

삶/은 계란이요

죽/은 대나무

난草/ 人가 !

YouTube 채널

번역한 책

블로그 내부검색

최신 포스팅

R: stand-alone R scpript using commandArgs()

Labels:


* 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.





Labels:





Scientist. Husband. Daddy. --- TOLLE. LEGE
[1] This blog complies with the "Fair Use Doctrine" as defined in Article 28 of the Republic of Korea Copyright Act and the U.S. Copyright Act (17 USC. §107). [2] Regarding copyrighted (© Kwangmin Choi) articles and translations, the following actions are prohibited (1) without providing a link to the original URL: (2) Full reproduction and distribution, (3) Unauthorized modification and arbitrary excerpting, and (4) Screen capturing and distribution. [3] Additionally, using the content of this blog for AI training is strictly prohibited. [운영] [대문으로] [방명록]
블로그 후원하기
[한국] 하나은행 (최광민): 376-910-500-183-07
[미국] 벤모 Venmo: @pay4kc
[국제] 페이팔 PayPal: @pay4kc