Statistics
| Revision:

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

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.txm.statsengine.r.core.RWorkspace
36

    
37

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

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

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

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

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