About me | pbdR Tech | HPSC | Phyloclustering | R_note |

 R_note -- The Exploration of Statistical Software R (統計軟體 R 深度歷險)
Reference
MS Windows

Batch jobs
Function
Data/MySQL
Plot
String/Parse

Remark Lines
Classes/S3
S4 Methods
Batch more
Environment

Good Coding
Fast Loop
LAM/MPI/Rmpi
Recursion

PHP Call R
Basic C
R Call Fortran/C
R Call GSL
C Call R API
C Call R Objects
Standalone

Make Packages
C Pointer
Debug

Section: Batch more

This section will talk about how we can batch jobs inside R scripts.

Examples:
1. assign, parse and eval

 ``` assign("a", 3:4) sum(a) sum(b <- 3:4) a b (cmd <- paste("sum(", "d", "<-", "3:4)", sep = " ")) eval(parse(text = cmd)) d ```

This is a silly example, but it gives a hint to process a command from a given string. The command `assign()` is similar to "<-", `parse()` can parse a string/text into an expression object which can be executed by `eval()`. At the above, "a", "b", and "d" are all the same as a vector contains 2 elements "3, 4".

2. call and eval

 ``` a <- function(x, y = 4, detail = FALSE){ ifelse(detail, x+y, 0) } b <- list(quote(a), quote(2), quote(3), quote(detail = TRUE)) mode(b) <- "call" b (d <- eval(b)) b[[4]] <- FALSE b (e <- eval(b)) ```

The object "b" is a "call" structure that can be used in eval(). The result for the object "d" should be 5, not 6, and the object "e" should be 0, as the following.

 ``` > b a(2, 3, TRUE) > (d <- eval(b)) [1] 5 > b[[4]] <- FALSE > b a(2, 3, FALSE) > (e <- eval(b)) [1] 0 ```

 [ Go to top ]

 Maintained: Wei-Chen Chen E-Mail: wccsnow @ gmail.com Last Revised: Dec 12 2016, 09:44 (CST Taipei, Taiwan) Created: Oct 06 2003 Best Resolution IE6.0 1280x1024 small font