2012年7月31日星期二

Batch R jobs - examples

http://www.math.ncu.edu.tw/~chenwc/R_note/index.php?item=batch

 Batch jobs

There are two examples for batching jobs in the page. The first one will demonstrate different batching method, and the second will demonstrate a batching method with extra arguments. Some of Linux knowledges are required for understanding these commands, and they are also useful when you have to process bunch of jobs. More ways to batch jobs can be found at the page "Batch more" in this web site and the page " Rscript" in the HPSC web site. 

Example 1:
  1. For MS Windows user
    Use Rterm.exe or Rcmd.exe to substitute R in the following command mode. 
  2. Hello world !
    First, create an R code file "hello_1.rcontains the following

        
    # File name: hello_1.r
    a <- c("Hello", "world", "!")
    print(a)
    b <- paste(a, collapse = " ")
    print(b)
    

    Hereit sets an array variable "acontains 3 elements, "Hello", "world", and "!", and prints themItalso uses the function "pasteto join all elements in array "aby " " (spaceand then it sets the arrayto a variable "band prints it
  3. Batch From Script
    Use the command mode to send "hello_1.r" as a batch job. 

        SHELL> R CMD BATCH --vanilla --slave hello_1.r output_file
    or
        SHELLnohup R CMD BATCH --vanilla --slave hello_1.r output_file &

    By default, the output will be saved in "hello_1.r.Rout" if an "output_file" isn't assigned. The output file (hello_1.r.Rout) will as the following. 

        
    > a <- c("Hello", "world", "!")
    > print(a)
    [1] "Hello" "world" "!"
    > b <- paste(a, collapse = " ")
    > print(b)
    [1] "Hello world !"
    >
    

  4. Batch From Command
    Use the command mode to send "hello_1.r" as a batch job from [STDIN] and show the output on [SDTOUT]. The followings have similar commands, and can provide same results, but they can be used in different purposes. 

        SHELL> R --vanilla --slave < hello_1.r > output_file
        or
        SHELLcat hello_1.r | R --vanilla --slave > output_file
        or
        SHELLecho 'source("hello_1.r")' | R --vanilla --slave > output_file

    By default, the output will be show on the screen, you can use ">" to redirect the output into "output_file". The output will like this. 

        
    [1] "Hello" "world" "!"
    [1] "Hello world !"
    


Example 2:
  1. Hello world, MOLAS !
    First, create an R code file "hello_2.r" contains the following. 

        
    # File name: hello_2.r
    a <- c("Hello", "world,", argv, "!")
    print(a)
    b <- paste(a, collapse = " ")
    print(b)
    

    Hereit sets an array variable "acontains 4 elements, "Hello", "world,", argvand "!", and printsthemThe variable "argvwill be passed from STDIN commandIt also uses the function "pastetojoin all elements in array "aby " " (spaceand sets to a variable "band prints it
  2. Pass Variable From STDIN
    Use command mode to send "hello_2.r" for batch job from STDIN and show the output on SDTOUT. The variable "argv" as "MOLAS" is assigned before the "hello_2.r" is passed to R. 

        SHELLecho 'argv <- "MOLAS"; source("hello_2.r")' | R --vanilla --slave > output_file

    By defaultthe output will be shown on the screenyou can use ">" to redirect the output into an"output_file". The output will like this

        
    [1] "Hello" "world," "MOLAS" "!"
    [1] "Hello world, MOLAS !"
    

  • Background process in Linux
    echo, nohup, &, |, <, > 

没有评论:

发表评论