Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / test / SomeRCode.groovy @ 479

History | View | Annotate | Download (2 kB)

1
/**
2
 * Main.
3
 *
4
 * @param args the args
5
 */
6
// Copyright © 2010-2013 ENS de Lyon.
7
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
8
// Lyon 2, University of Franche-Comté, University of Nice
9
// Sophia Antipolis, University of Paris 3.
10
// 
11
// The TXM platform is free software: you can redistribute it
12
// and/or modify it under the terms of the GNU General Public
13
// License as published by the Free Software Foundation,
14
// either version 2 of the License, or (at your option) any
15
// later version.
16
// 
17
// The TXM platform is distributed in the hope that it will be
18
// useful, but WITHOUT ANY WARRANTY; without even the implied
19
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20
// PURPOSE. See the GNU General Public License for more
21
// details.
22
// 
23
// You should have received a copy of the GNU General
24
// Public License along with the TXM platform. If not, see
25
// http://www.gnu.org/licenses.
26
// 
27
// 
28
// 
29
// $LastChangedDate:$
30
// $LastChangedRevision:$
31
// $LastChangedBy:$ 
32
//
33
package org.txm.test
34

    
35
import org.rosuda.REngine.REXP;
36
import org.txm.Toolbox;
37
import org.txm.statsengine.r.core.RWorkspace;
38

    
39
// TODO: Auto-generated Javadoc
40
/* (non-Javadoc)
41
 * @see groovy.lang.Script#run()
42
 */
43
RWorkspace rw = RWorkspace.getRWorkspaceInstance();//get the RWorkspace instance
44
rw.setLog(true);//start R log
45

    
46
//execute a simple line of code
47
rw.eval("print(2+2)")
48
println "-"
49

    
50
//execute a file
51
/*
52
 * script.R:
53
a <- 2+2
54
print(a)
55
b <- 4+4
56
print(b)
57
c <- 6+6
58
print(c)
59
 */
60
File scriptR = new File("script.R")// get a File
61
String filepath = scriptR.getAbsolutePath().replace("\\","\\\\")//We need to to it for Windows OS
62
rw.eval("source(\""+filepath+"\")")
63
println "-"
64

    
65
//execute a file line per line
66
Reader reader = new FileReader(scriptR);// create a file reader
67
String line = reader.readLine();// read next line
68
while(line != null)//continue until the end of the file
69
{
70
        rw.eval(line)//evaluate line in R
71
        line = reader.readLine();//read next line
72
}
73
println "-"
74

    
75
println "Last log line : "+rw.getLastLogLine();// get the last log
76
rw.setLog(false);//stop R log