ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/tools/R.java
Revision: 9
Committed: Tue Feb 14 11:00:40 2006 UTC (18 years, 7 months ago) by filippis
File size: 5012 byte(s)
Log Message:
Adding R class
Line File contents
1 package tools;
2
3 import tools.*;
4 import java.io.*;
5 import java.util.*;
6 import org.rosuda.JRclient.*;
7 import Jama.*;
8
9 public class R {
10
11 private static Rconnection c = null;
12 private static String mySqlConfString = null;
13
14 public R(String username, String password, String connFile) {
15
16 try {
17 c = new Rconnection();
18 c.login(username, password);
19 mySqlConfString = readConnectionFile(connFile);
20 // treat warnings as errors
21 c.voidEval("options(warn=0)");
22 c.voidEval("library(RMySQL);drv<-dbDriver(\"MySQL\")");
23 } catch(RSrvException rse) {
24 System.out.println("Rserve exception: "+rse.getMessage());
25 cleanUp();
26 } catch(Exception e) {
27 System.out.println("Something went wrong, but it's not the Rserve: "+e.getMessage());
28 e.printStackTrace();
29 cleanUp();
30 }
31
32 }
33
34 public double[][] simpleLinRegr(String predictorQuery, String responseQuery) {
35
36 //predictor = intercept + slope*response
37
38 double[][] lr = null;
39 boolean singlePredictor = true;
40
41 try {
42
43 c.voidEval("con<-dbConnect(drv, "+mySqlConfString+")");
44 c.voidEval("x<-dbGetQuery(con, \""+predictorQuery+"\")");
45 singlePredictor = c.eval("ncol(x)==1").asBool().isTRUE();
46
47 if (!singlePredictor) {
48 c.voidEval("discon<-dbDisconnect(con)");
49 return lr;
50 }
51
52 c.voidEval("x<-x[,1]");
53 c.voidEval("rs<-dbGetQuery(con, \""+responseQuery+"\")");
54 c.voidEval("lr.m<-matrix(0, ncol(rs), 4)");
55
56 c.voidEval("for (j in 1:ncol(rs)) {"+
57 " y<-rs[,j];"+
58 " model<-lm(y~x);"+
59 " info<-summary(model);"+
60 " lr.m[j,1:2]<-model$coefficients;"+ //intercept - slope
61 " lr.m[j,3]<-info$coefficients[2,4];"+ //p-value
62 " lr.m[j,4]<-sqrt(info$r.squared);"+ //r
63 "}");
64
65 lr = c.eval("lr.m").asDoubleMatrix();
66
67 c.voidEval("discon<-dbDisconnect(con)");
68
69 } catch(RSrvException rse) {
70 System.out.println("Rserve exception: "+rse.getMessage());
71 cleanUp();
72 } catch(Exception e) {
73 System.out.println("Something went wrong, but it's not the Rserve: "+e.getMessage());
74 e.printStackTrace();
75 cleanUp();
76 }
77
78 return lr;
79
80 }
81
82 private static void cleanUp() {
83
84 try {
85 c.voidEval("try(discon<-dbDisconnect(con), silent = TRUE)");
86 c.voidEval("dbUnloadDriver(drv)");
87 } catch(RSrvException rse) {
88 System.out.println("Rserve exception: "+rse.getMessage());
89 } finally {
90 if (c != null) {
91 c.close();
92 }
93 }
94 }
95
96 public void close() {
97
98 try {
99 c.voidEval("try(discon<-dbDisconnect(con), silent = TRUE)");
100 c.voidEval("dbUnloadDriver(drv)");
101 } catch(RSrvException rse) {
102 System.out.println("Rserve exception: "+rse.getMessage());
103 } finally {
104 if (c != null) {
105 c.close();
106 }
107 }
108 }
109
110 private static String readConnectionFile(String connFile) {
111
112 FileReader theFile = null;
113 BufferedReader fileIn = null;
114 StringTokenizer str;
115 String item, oneLine, dummy;
116 String host = null, port = null, user = null, password = null, dbname = null;
117
118 // list the entries in the file and decompose them
119 try {
120 File inputFile = new File(connFile);
121 theFile = new FileReader(inputFile); // open the File
122 fileIn = new BufferedReader( theFile); // open BufferedReader
123 while ((oneLine = fileIn.readLine() ) != null ) {
124 // Write the line at hand to stdout, just for testing purposes
125 // System.out.println("["+oneLine+"]");
126 // Construct a stringTokenizer for the line that we read with : delimited
127 str = new StringTokenizer( oneLine, " ="); // true sets returnDelimiters flag
128 while ( str.hasMoreTokens()) {
129 item = str.nextToken();
130 // System.out.println("item:"+item);
131 if( item.equals("host")) {
132 host=str.nextToken();
133 // System.out.println("host:"+host);
134 break;
135 } // end if host
136 if( item.equals("port")) {
137 port=str.nextToken();
138 // System.out.println("port:"+port);
139 break;
140 } // end if port
141 if( item.equals("user")) {
142 user=str.nextToken();
143 // System.out.println("user:"+user);
144 break;
145 } // end if password
146 if( item.equals("password")) {
147 password=str.nextToken();
148 // System.out.println("password:"+password);
149 break;
150 } // end if password
151 if( item.equals("database")) {
152 dbname=str.nextToken();
153 // System.out.println("database:"+dbname);
154 break;
155 } // end if password
156
157 } // next token in this line
158 } // next line in the file
159 } // end try opening the file
160 catch ( Exception e ) { System.out.println( e); }
161
162 try { // closing the file
163 if( fileIn != null) fileIn.close();
164 if( theFile != null) theFile.close();
165 } catch ( Exception e ) { System.out.println( e); }
166
167 return ("username = \""+user+"\", password = \""+password+"\", dbname = \""+dbname+"\", host = \""+host+"\", port = "+port);
168
169 } // end class readConnectionFile
170
171 } // end class mySQLconnect

Properties

Name Value
svn:executable *