File: RMethods.java

package info (click to toggle)
metastudent 2.0.1-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 95,644 kB
  • sloc: java: 3,287; perl: 2,089; python: 1,421; ruby: 242; sh: 39; makefile: 19
file content (97 lines) | stat: -rwxr-xr-x 2,098 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package pp2.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Class for the writing of R scripts and running these from the code
 * @author Stefanie Kaufmann
 *
 */
public class RMethods {
	
	private static String installationPath;
	private File script;
	private BufferedWriter buff;
	private File outputFile = new File("temp.out");

	public RMethods(String rPath) {
		installationPath = rPath;
	}
	
	/**
	 * Start R with the previously written script
	 */
	public void runR() {
		try {
			Runtime rt   = java.lang.Runtime.getRuntime();
			String separator = System.getProperty("file.separator");
			//kickstart R with script file
			Process proc = rt.exec(installationPath+separator+"R CMD BATCH "+script.getAbsolutePath()+" "+outputFile.getAbsolutePath());
			
			int exitVal  = proc.waitFor();
			System.out.println(exitVal);
			//remove temporary files
			script.deleteOnExit();
			outputFile.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Start to write the R script
	 * @param filename The name of the script file
	 */
	public void startRScript(String filename) {
		try {
			script = new File(filename);
			if(script.exists()) {
				script.delete();
				script = new File(filename);
			}
			buff   = new BufferedWriter(new FileWriter(script));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * End the writing of the R script
	 */
	public void endRScript() {
		try {
			buff.flush();
			buff.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Write one line into the R script
	 * @param line The line that will be written into the script
	 */
	public void addLineToScript(String line) {
		try {
			buff.write(line+"\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Define the installation path of R
	 * @param path The path of the bin directory where R is installed
	 */
	public void setPath(String path) {
		installationPath = path;
	}
	

}