Statistics
| Revision:

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

History | View | Annotate | Download (2 kB)

1 321 mdecorde
/**
2 321 mdecorde
 * Main.
3 321 mdecorde
 *
4 321 mdecorde
 * @param args the args
5 321 mdecorde
 */
6 321 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
7 321 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
8 321 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
9 321 mdecorde
// Sophia Antipolis, University of Paris 3.
10 321 mdecorde
//
11 321 mdecorde
// The TXM platform is free software: you can redistribute it
12 321 mdecorde
// and/or modify it under the terms of the GNU General Public
13 321 mdecorde
// License as published by the Free Software Foundation,
14 321 mdecorde
// either version 2 of the License, or (at your option) any
15 321 mdecorde
// later version.
16 321 mdecorde
//
17 321 mdecorde
// The TXM platform is distributed in the hope that it will be
18 321 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
19 321 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 321 mdecorde
// PURPOSE. See the GNU General Public License for more
21 321 mdecorde
// details.
22 321 mdecorde
//
23 321 mdecorde
// You should have received a copy of the GNU General
24 321 mdecorde
// Public License along with the TXM platform. If not, see
25 321 mdecorde
// http://www.gnu.org/licenses.
26 321 mdecorde
//
27 321 mdecorde
//
28 321 mdecorde
//
29 321 mdecorde
// $LastChangedDate:$
30 321 mdecorde
// $LastChangedRevision:$
31 321 mdecorde
// $LastChangedBy:$
32 321 mdecorde
//
33 321 mdecorde
package org.txm.test
34 321 mdecorde
35 479 mdecorde
import org.rosuda.REngine.REXP;
36 479 mdecorde
import org.txm.Toolbox;
37 479 mdecorde
import org.txm.statsengine.r.core.RWorkspace;
38 321 mdecorde
39 321 mdecorde
// TODO: Auto-generated Javadoc
40 321 mdecorde
/* (non-Javadoc)
41 321 mdecorde
 * @see groovy.lang.Script#run()
42 321 mdecorde
 */
43 321 mdecorde
RWorkspace rw = RWorkspace.getRWorkspaceInstance();//get the RWorkspace instance
44 321 mdecorde
rw.setLog(true);//start R log
45 321 mdecorde
46 321 mdecorde
//execute a simple line of code
47 321 mdecorde
rw.eval("print(2+2)")
48 321 mdecorde
println "-"
49 321 mdecorde
50 321 mdecorde
//execute a file
51 321 mdecorde
/*
52 321 mdecorde
 * script.R:
53 321 mdecorde
a <- 2+2
54 321 mdecorde
print(a)
55 321 mdecorde
b <- 4+4
56 321 mdecorde
print(b)
57 321 mdecorde
c <- 6+6
58 321 mdecorde
print(c)
59 321 mdecorde
 */
60 321 mdecorde
File scriptR = new File("script.R")// get a File
61 321 mdecorde
String filepath = scriptR.getAbsolutePath().replace("\\","\\\\")//We need to to it for Windows OS
62 321 mdecorde
rw.eval("source(\""+filepath+"\")")
63 321 mdecorde
println "-"
64 321 mdecorde
65 321 mdecorde
//execute a file line per line
66 321 mdecorde
Reader reader = new FileReader(scriptR);// create a file reader
67 321 mdecorde
String line = reader.readLine();// read next line
68 321 mdecorde
while(line != null)//continue until the end of the file
69 321 mdecorde
{
70 321 mdecorde
        rw.eval(line)//evaluate line in R
71 321 mdecorde
        line = reader.readLine();//read next line
72 321 mdecorde
}
73 321 mdecorde
println "-"
74 321 mdecorde
75 321 mdecorde
println "Last log line : "+rw.getLastLogLine();// get the last log
76 321 mdecorde
rw.setLog(false);//stop R log