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.r" contains the following. 

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

    Here, it sets an array variable "a" contains 3 elements, "Hello", "world", and "!", and prints them. Italso uses the function "paste" to join all elements in array "a" by " " (space) and then it sets the arrayto a variable "b" and 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
        SHELL> nohup 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
        SHELL> cat hello_1.r | R --vanilla --slave > output_file
        or
        SHELL> echo '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)
    

    Here, it sets an array variable "a" contains 4 elements, "Hello", "world,", argv, and "!", and printsthem. The variable "argv" will be passed from STDIN command. It also uses the function "paste" tojoin all elements in array "a" by " " (space) and sets to a variable "b" and 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. 

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

    By default, the output will be shown on the screen, you 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, &, |, <, > 

没有评论:

发表评论