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