Monday 29 June 2015

R: source : Read R code from a file (or) connection

By using source() function, you can read R code from a file (or) a connection.  It is inverse of dump function. Source function reads, parse the expressions and execute them.

Syntax
source(file, local = FALSE, echo = verbose, print.eval = echo,
       verbose = getOption("verbose"),
       prompt.echo = getOption("prompt"),
       max.deparse.length = 150, chdir = FALSE,
       encoding = getOption("encoding"),
       continue.echo = getOption("continue"),
       skip.echo = 0, keep.source = getOption("keep.source"))

I don’t want to explain each and every argument separately. If you install R in your system, just call help(“command”) from R console.

Let’s say “dump.txt” contains following data.

var1 <-
10
var2 <-
"1234"
var3 <-

1:10


> var1<- var2<- var3<- 0
> 
> var1
[1] 0
> var2
[1] 0
> var3
[1] 0
> 
> source("dump.txt")
> 
> var1
[1] 10
> var2
[1] "1234"
> var3
 [1]  1  2  3  4  5  6  7  8  9 10


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment